How to use the super keyword and instanceof operator in Java

Java super keyword super keyword is similar to this keyword. This is used to represent the instance of the current class and super is used to represent the parent class.

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

The parent class behavior is called as if it is the behavior of this class. Moreover, the calling behavior does not have to occur in the parent class, and it can automatically trace back to the upper class.

Function of super keyword: call variables declared as private in the parent class. Click the method that has been covered. Represents the parent class constructor as a method name. Call hidden variables and overridden methods

Operation results:

The move () method can also be defined in some ancestor classes, such as the parent class of the parent class. Java has traceability and will look up until the method is found.

When calling the hidden variable of the parent class through super, you must declare the getter method in the parent class, because the data member declared as private is invisible to the child class. Call 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 display the constructor that calls the parent class.

Running result: I'm a lovely dog. My name is Huahua. I'm 3 years old

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

It should be noted that another method of construction is invoked in the construction method, and the action must be placed at the very beginning. A constructor cannot be called inside any method other than a constructor. Only one constructor can be called within a constructor.

If you write a constructor that calls neither super () nor this (), the compiler will automatically insert a call into the parent constructor without parameters.

Finally, pay attention to 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 the parent class method.

The polymorphism of Java instanceof operator brings a problem, that is, how to judge the type of object actually referenced by a variable. C + + uses runtime type information (RTTI), and Java uses the instanceof operator.

The instanceof operator is used to judge the actual type of the object referenced by a variable. Note that it refers to the type of the object, not the type of the variable. See the following code:

Operation results:

It can be seen that if the variable references an instance of the current class or its subclass, instanceof returns true; otherwise, it returns false.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>