1, Variable hiding - variables are not polymorphic
Variables in Java do not follow polymorphism, so rewriting applies only to methods, not variables. Moreover, when the instance variable in the child class has the same name as the instance variable in the parent class, select the instance variable from the reference type.
In Java, when the instance variable in the subclass has the same name as the instance variable in the parent class, the variables of the subclass will hide the variables of the parent class, even if their types are different. This concept is called variable hiding.
In variable hiding, a subclass hides inherited variables rather than replacing them, which basically means that the object of the subclass contains two variables, while the child variable hides the parent variable. We can use super X to access variables in the parent class.
Example:
class Parent { String x = "Parent`s Instance Variable"; public void print() { System.out.println(x); } } class Child extends Parent { String x = "Child`s Instance Variable"; @Override public void print() { System.out.print(x); // You can use super X accesses variables whose parent class is hidden System.out.print(", " + super.x + "\n"); } } Parent parent = new Child(); System.out.println(parent.x) Child child = new Child(); System.out.println(child.x)
result:
Parent`s Instance Variable Child`s Instance Variable
Resolution:
In Java, when we define a variable in the Child class with the name that has been used to define the variable in the Parent class, the variable of the Child class will hide the variable of the Parent class. Therefore, when we try to access the variable from the reference of the Parent object by holding the Child object, the variable in the Parent class will be called. Therefore, we know that instance variables are selected from reference types rather than instance types, and polymorphism does not apply to variables.
Why are variables designed to follow hiding rather than overwriting?
Because if we change its type in a subclass, variable overrides may break methods inherited from the parent.
Each subclass inherits variables and methods (state and behavior) from its parent class. If Java allows variable overrides, it destroys any methods that use the variable, and the compiler will give errors in the subclass because the child has inherited these methods from the parent.
2, Method rewriting -- methods are polymorphic
In the case of method override, the override method completely replaces the inherited method, so when we try to access the method from the reference of the parent object by holding the child object, the method in the child class will be called. We can use super X () to access the methods in the parent class.
3, Use of super
1. The subclass calls the variables of the hidden parent class
At this time, the subclass has the same field as the parent class (the parent class field is hidden). In order to obtain this field of the parent class, we must add super.
We cannot access the private modified variable of the parent class through super, because this only belongs to the internal member of the parent class, and an object cannot access its private member.
Example:
public class A { String nameA="A"; } public class B extends A{ String nameA="B"; public void getName() { System.out.println("Subclass"+nameA); System.out.println("Parent class"+super.nameA); } public static void main(String[] args) { B b=new B(); b.getName(); }
2. The subclass calls the method of the overridden parent class
In the subclass, we override the method of the parent class. If we want to call the same method of the parent class, we must indicate it through the super keyword. If it is not clear, according to the principle of subclass priority, it is equivalent to calling the overridden method again
Example:
public class A { private String nameA="A"; public void getName() { System.out.println("Parent class"+nameA); } public static void main(String[] args) { } } public class B extends A{ private String nameB="B"; @Override public void getName() { System.out.println("Subclass"+nameB); super.getName(); } public static void main(String[] args) { B b=new B(); b.getName(); } }
3. Call the constructor of the parent class
Because the constructor of the parent class cannot be inherited, the compiler will automatically add super() to the first sentence of the subclass constructor to call the parameterless constructor of the parent class. At this time, it can be omitted. If you want to write it, it must be in the first sentence of the subclass constructor. You can call other overloaded construction methods of the parent class through super, as long as you pass the corresponding parameters.
Note: when a subclass inherits the parent class, it will automatically inherit the default constructor of the parent class (that is, inherit the parameterless constructor). If your class already has a constructor with parameters, and you don't write the default constructor without parameters, the subclass will report an error when inheriting, because the system doesn't know which constructor to inherit. You must explicitly use the super () keyword to describe it. Therefore, in order to avoid this error, we usually write a constructor without parameters in a class with multiple constructors.
Example:
public class A { private String nameA="A"; public A() { } public A(String nameA) { this.nameA = nameA; } } public class B extends A{ private String nameB="B"; public B() { super("lhj"); } } }