杰瑞科技汇

Java中super.super如何调用父类的父类方法?

Of course! The phrase "Java super super" is not a standard Java keyword or command. It's likely a reference to the super keyword used in a specific, advanced way, or a typo for something else.

Java中super.super如何调用父类的父类方法?-图1
(图片来源网络,侵删)

Let's break down what super means in Java, from the basics to the more advanced "super super" cases.


Part 1: The Core Meaning of super

The super keyword in Java is a reference variable that is used to refer to the parent class (also called the superclass or base class) object. It's primarily used for three things:

  1. Accessing Parent Class Members: To access a field or method from the parent class that is hidden by a member in the child class.
  2. Invoking Parent Class Constructors: To call the constructor of the parent class from the child class's constructor.
  3. Invoking Parent Class Methods: To explicitly call a method defined in the parent class.

Accessing Parent Class Members

If a child class has a field or method with the same name as in the parent class, the super keyword is used to distinguish between them.

class Animal {
    String name = "Animal from Parent";
    void display() {
        System.out.println("This is the Animal class display method.");
    }
}
class Dog extends Animal {
    // This 'name' hides the 'name' in the Animal class
    String name = "Dog from Child";
    void printName() {
        // Accessing the child class's 'name'
        System.out.println("Child class name: " + this.name);
        // Accessing the parent class's 'name' using 'super'
        System.out.println("Parent class name: " + super.name);
    }
}
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.printName();
        myDog.display(); // Calls the parent's method because it's not overridden
    }
}

Output:

Child class name: Dog from Child
Parent class name: Animal from Parent
This is the Animal class display method.

Invoking Parent Class Constructors

This is the most common use of super. The call to super() must be the first statement in a subclass's constructor. If you don't write it explicitly, the compiler automatically inserts a call to the no-argument constructor of the parent class (super()).

class Vehicle {
    Vehicle() {
        System.out.println("Vehicle's no-arg constructor is called.");
    }
}
class Car extends Vehicle {
    Car() {
        // This call to super() is implicit if not written.
        // It calls Vehicle's constructor.
        super(); // Explicit call
        System.out.println("Car's constructor is called.");
    }
}
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
    }
}

Output:

Vehicle's no-arg constructor is called.
Car's constructor is called.

Invoking Parent Class Methods

This is useful when you want to extend the functionality of a parent's method rather than completely replacing it (overriding).

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}
class Horse extends Animal {
    @Override // Good practice to use @Override annotation
    void eat() {
        // First, call the parent's original eat() method
        super.eat();
        // Then, add new functionality specific to Horse
        System.out.println("This horse also eats hay.");
    }
}
public class Main {
    public static void main(String[] args) {
        Horse myHorse = new Horse();
        myHorse.eat();
    }
}

Output:

This animal eats food.
This horse also eats hay.

Part 2: The "Super Super" Meaning (Advanced)

When people say "Java super super," they are almost always referring to a situation where you have multiple levels of inheritance and need to access a member from the "grandparent" class or higher.

Let's create a class hierarchy: Animal (grandparent) -> Mammal (parent) -> Dog (child).

class Animal {
    String name = "Animal Grandparent";
    void show() {
        System.out.println("This is the Animal class show() method.");
    }
}
class Mammal extends Animal {
    // This 'name' hides the one in Animal
    String name = "Mammal Parent";
    void show() {
        System.out.println("This is the Mammal class show() method.");
    }
}
class Dog extends Mammal {
    // This 'name' hides the one in Mammal
    String name = "Dog Child";
    void display() {
        // 'this.name' refers to the Dog class's 'name'
        System.out.println("Dog's name: " + this.name);
        // 'super.name' refers to the immediate parent's (Mammal) 'name'
        System.out.println("Mammal's name: " + super.name);
        // 'super.super' is NOT valid syntax in Java!
        // You CANNOT directly access the grandparent's 'name' like this.
        // System.out.println("Animal's name: " + super.super.name); // COMPILE ERROR!
    }
    void callGrandparentMethod() {
        // To call the grandparent's show() method, you must first
        // call the parent's overridden show() method, and from there,
        // use super to call the grandparent's method.
        super.show(); // This calls Mammal's show()
    }
}
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.display();
        myDog.callGrandparentMethod();
    }
}

The "Super Super" Problem and Solution

As you can see in the commented-out line, super.super is not a valid construct in Java. You cannot directly access a member two levels up the inheritance chain.

The standard workaround, as shown in the callGrandparentMethod() example, is to chain the calls:

  1. From the Dog class, call super.show(). This invokes the show() method in the immediate parent, Mammal.
  2. Inside the Mammal class's show() method, you can then use super.show() to invoke the show() method in its parent, Animal.

Here is how you would implement it correctly:

class Animal {
    void show() {
        System.out.println("This is the Animal class show() method.");
    }
}
class Mammal extends Animal {
    @Override
    void show() {
        // Use super to call the parent's (Animal's) show() method
        super.show();
        System.out.println("This is the Mammal class show() method.");
    }
}
class Dog extends Mammal {
    // To "get to" the grandparent's method, we call the parent's method,
    // which in turn calls the grandparent's.
    void callGrandparentMethod() {
        super.show(); // This will execute the Mammal's show() method
    }
}
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.callGrandparentMethod();
    }
}

Output:

This is the Animal class show() method.
This is the Mammal class show() method.

Part 3: Other Possible Meanings of "Java super super"

While the multi-level inheritance scenario is the most likely meaning, here are two other possibilities:

A Typo for super() vs. super

This is a very common point of confusion for beginners.

  • super(): This is a constructor call. It must be the first line of a constructor and is used to create and initialize the parent part of the object.
  • super: This is a reference variable. It's used to access members (fields or methods) of the parent class.

The super Keyword in Interfaces (Java 9+)

Since Java 9, interfaces can have private methods. These private methods can be static or instance methods. They are not inherited and are only accessible within the interface itself.

However, an interface can also have static methods that are public. There is no concept of a "super interface" in the same way there is a "super class," so super is not used with interfaces in this context. This is likely not what "super super" refers to.

Summary

Term Meaning Example
super The standard keyword to refer to the immediate parent class. super.method(), super.field, super()
super super (Not Valid Java) A conceptual term for accessing a grandparent class's member. super.super.name // COMPILE ERROR
"Super Super" Solution The workaround of chaining super calls through the immediate parent. In Dog class: super.show() calls Mammal's method. Inside Mammal's method, super.show() calls Animal's method.

In short, when you hear "Java super super," think multi-level inheritance and remember that you cannot directly access a grandparent's member. You must navigate through the parent class to get there.

分享:
扫描分享到社交APP
上一篇
下一篇