Java Super keyword
The Super keyword is similar to this, this is used to represent an instance of the current class, and super is used to represent the parent class.
Super can be used in subclasses, by Dot (.) To get the member variables and methods of the parent class. Super can also be used in subclasses of subclasses, and Java can automatically trace back to the upper class.
The parent class behavior is invoked as if the behavior were the behavior of this class, and the invocation behavior does not have to occur in the parent class, and it can automatically trace back to the upper class.
Features of the Super keyword:
Invokes a variable declared private in the parent class.
Point to the method that has already been overridden.
Represents the parent class construction method as a method name.
Calling hidden variables and overridden 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";
You must 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 ();//Calling the method of the parent class
System.out.println ("Dogs can walk and run ");
Invoke the parent class hidden variable
System.out.println ("Please remember:" + SUPER.GETDESC ()) through the Getter method;
}
Run Result:
Animals can move
Dogs can walk and run please
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, and Java is retrospective, looking up until the method is found.
To invoke a hidden variable of the parent class through super, you must declare the Getter method in the parent class, because the data member declared private is not visible to the child class.
To invoke the constructor method of the parent class
In many cases, the default construction method is used to initialize the parent class object. Of course, you can also use super to display the constructor method that invokes the parent class.
public class demo{public
static void Main (string[] args) {
Dog obj = new Dog ("Flower", 3);
Obj.say ();
}
Class animal{
String name;
Public Animal (String name) {
this.name = name;
}
}
Class Dog extends animal{
int age;
Public Dog (String name, int age) {
super (name);
This.age = age;
}
public void Say () {
System.out.println ("I am a cute puppy, my name is" + name + ", I" + Age + "old");
}
Run Result:
I am a lovely puppy, my name is flower, I am 3 years old.
Note: either super () or this () must be placed on the first line of the construction method.
It is noteworthy that:
To invoke another constructor in the constructor method, the invocation action must be placed at the beginning.
You cannot call a constructor method within any method other than the constructor method.
Only one constructor method can be called within a constructor.
If you write a construction method that does not call super () or call this (), the compiler automatically inserts a call into the parent class constructor and takes no arguments.
Finally, notice the difference between super and this: super is not a reference to an object, you cannot assign super to another object variable, it's just a special keyword that instructs the compiler to invoke the parent class method.
Java instanceof operator
Polymorphism poses the problem of how to determine the type of object that a variable actually refers to. 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, noting the type of object it refers to, not the type of the variable. Take a look at the following code:
Public final class demo{public
static void Main (string[] args) {
//References instance of people class
people obj = new people ();
if (obj instanceof object) {
System.out.println ("I am an Object");
}
if (obj instanceof people) {
System.out.println ("I am Human");
}
if (obj instanceof Teacher) {
System.out.println ("I am a Teacher");
}
if (obj instanceof President) {
System.out.println ("I am Principal");
}
System.out.println ("-----------");
the line//reference instance of the 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 Human");
}
if (obj instanceof Teacher) {
System.out.println ("I am a Teacher");
}
if (obj instanceof President) {
System.out.println ("I am Principal");
}} Class people{}
class Teacher extends people{}
class President extends teacher{}
Run Result:
I am an object I am a
human-----------I am
an object I
am a human I am a
teacher
As you can see, if the variable refers to an instance of the current class or its subclass, instanceof returns TRUE, otherwise it returns false.