Java super keywords
The super keyword is similar to this, which is used to represent an instance of the current class, and super is used to represent a parent class.
super can be used in subclasses to get member variables and methods of the parent class through dots (.). super can also be used in subclasses of subclasses, and Java can automatically trace the upper class.
The parent class behavior is called, just as if the behavior is the behavior of this class, and the calling behavior does not have to happen in the parent class, it can automatically trace the upper class.
Functions of the super keyword:
Calls a variable declared as private in the parent class.
Click the method that has been covered.
Represents the parent class constructor as the method name.
Call hidden variables and overwritten methods
public class Demo{ public static void main(String[] args) { Dog obj = new Dog(); obj.move(); }}class Animal{ private String desc = "Animals are human's good friends"; // Must be Declare a getter method public String getDesc() { return desc; } public void move(){ System.out.println("Animals can move"); }}class Dog extends Animal { public void move(){ super.move( ); // Call the parent class's method System.out.println("Dogs can walk and run"); // Call the parent class's hidden variable System.out.println("Please remember: " + super.getDesc( )); }}
Running results:
Animals can moveDogs can walk and runPlease remember: Animals are human's good friends
The move() method can also be defined in some ancestor classes, such as the parent class of the parent class. Java is traceable and will keep looking up until the method is found.
To call hidden variables of the parent class through super, you must declare the getter method in the parent class, because the data members declared as private are not visible to the subclass.
Call the constructor of the parent class
In many cases, the default constructor is used to initialize the parent class object. Of course, you can also use super to display the constructor that calls the parent class.
public class Demo{ public static void main(String[] args) { Dog obj = new Dog("Huahua", 3); obj.say(); }}class Animal{ String name; public Anima l(String name){ this .name = name; }}class Dog extends Animal{ int age; public Dog(String name, int age){ super(name); this.age = age; } public void says(){ Sy stem.out.println(" I'm a cute puppy, my name is "+ name + ", I" + age + "year-old"); }}
Running results:
I am a cute puppy, my name is Huahua, I am 3 years old
Note: Whether it is super() or this(), it must be placed in the first line of the constructor.
It is worth noting:
Another constructor is called in the constructor, and the call action must be placed at the starting position.
A constructor cannot be called within any method other than a constructor.
Only one constructor can be called within a constructor.
If you write a constructor without calling super() or this(), the compiler will automatically insert a call to the parent class constructor without parameters.
Finally, note the difference between super and this: super is not a reference to an object, and super cannot be assigned to another object variable. It is just a special keyword that indicates that the compiler calls the parent class method.
Java instanceof operator <br />Polymorphism brings a problem, which is how to judge the type of the object actually referenced by a variable. C++ uses runtime-type information(RTTI), and Java uses the instanceof operator.
The instanceof operator is used to determine the actual type of the object referenced by a variable. Note that the type of the object it refers to is not the type of the variable. Please see the following code:
public final class Demo{ public static void main(String[] args) { // Reference the instance of People class People obj = new People(); if(obj instanceof Object){ System.out.pr intln("I am an object" ); } if(obj instanceof People){ System.out.println("I am a human"); } if(obj instanceof Teacher){ System.out.println("I am a teacher"); } if(obj instanceof President){ System.out.println("I am the principal"); } System.out.println("----------------"); // Dividing line// Examples refer to Teacher class obj = new Teacher(); if(obj instanceof Object){ System.out.println("I am an object"); } if(obj instanceof People){ System.out.println("I am a human"); } if(obj instanceof Teacher){ System.out.println("I am a teacher"); } if(obj instanceof President){ System.out.println("I am the principal"); } }}class People{ } : : : : : : : : : : : : : : : class Teacher extends People{ }class President extends Teacher{ }
Running results:
I am an object I am a human ------------------------------------------------------------------------------------------------------------------------
As can be seen, instanceof returns true if the variable refers to an instance of the current class or its subclass, otherwise false.