The Java super keyword and the instanceof operator use methods

  • 2020-04-01 04:13:50
  • OfStack

Java super keyword
The super keyword is similar to this, which represents the instance of the current class, and super, which represents the parent class.

Super can be used in subclasses to get member variables and methods of a parent class by the dot (.). Super can also be used in subclasses of subclasses, and Java can automatically trace back to the upper class.

The parent behavior is invoked as if the behavior were the behavior of the class, and the invocation behavior does not have to occur in the parent class, it can automatically be traced back to the upper class.

Function of super keyword:
Invokes a variable declared private in the parent class.
Click on the method that has been overridden.
Represents the parent constructor as a method name.
Calls 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(); //Calls a method of the parent class
    System.out.println("Dogs can walk and run");
    //The parent class is called by the getter method to hide the variable
    System.out.println("Please remember: " + super.getDesc());
  }
}

Operation results:


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 of a parent class, and Java is traceable and looks up until it finds the method.

Calling a hidden variable in the parent class through super requires you to declare getter methods in the parent class because data members declared private are invisible to subclasses.
Invokes the constructor of the parent class

In many cases, the default constructor is used to initialize the parent object. Of course, you can also use super to show the constructor that invokes the parent class.


public class Demo{
  public static void main(String[] args) {
    Dog obj = new Dog(" Flower 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 lovely puppy, my name is " + name + " , I " + age + " At the age of ");
  }
}

Operation results:
I am a lovely puppy, my name is hua hua, I am 3 years old

Note: both super() and this() must be placed on the first line of the constructor.

It is worth noting that:
To invoke another constructor in a constructor, the invoke action must be placed at the beginning.
A constructor cannot be invoked in any method other than a constructor.
Only one constructor can be called within a constructor.

If you write a constructor that neither calls super() nor this(), the compiler automatically inserts a call into the parent constructor with no arguments.

Finally, note the difference between super and this: super is not a reference to an object and cannot be assigned to another object variable; it is just a special keyword that instructs the compiler to call a superclass method.

Java instanceof operator
Polymorphism raises the question 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 that a variable refers to, note that it refers to the type of the object, not the type of the variable. Look at the following code:


public final class Demo{
  public static void main(String[] args) {
    //Refer to an instance of the People class
    People obj = new People();
    if(obj instanceof Object){
      System.out.println(" I'm an object ");
    }
    if(obj instanceof People){
      System.out.println(" I am a human being ");
    }
    if(obj instanceof Teacher){
      System.out.println(" I'm a teacher ");
    }
    if(obj instanceof President){
      System.out.println(" I am the principal ");
    }
    System.out.println("-----------"); //Dividing line
    
    //Refer to an instance of the Teacher class
    obj = new Teacher();
    if(obj instanceof Object){
      System.out.println(" I'm an object ");
    }
    if(obj instanceof People){
      System.out.println(" I am a human being ");
    }
    if(obj instanceof Teacher){
      System.out.println(" I'm a teacher ");
    }
    if(obj instanceof President){
      System.out.println(" I am the principal ");
    }
  }
}
class People{ }
class Teacher extends People{ }
class President extends Teacher{ }

Operation results:


 I'm an object 
 I am a human being 
-----------
 I'm an object 
 I am a human being 
 I'm a teacher 

As you can see, instanceof returns true if the variable references an instanceof the current class or its subclasses, or false otherwise.


Related articles: