As for the difference between hiding and overwriting, refer to the RTTI (run-time type identification), the Run-time polymorphism, when a parent class references a subclass object, see the following code I wrote:
Copy Code code as follows:
public class RunTime {
public static void Main (string[] args) {
Animal a = new Cat ();
System.out.println (A.A);
System.out.println (A.B);
A.voice ();
A.method ();
}
}
Class Dog extends Animal {
public int b = 3;
public static int A = 3;
public static void Method () {
System.out.println ("dog");
}
public void Voice () {
System.out.println ("dog called");
}
}
Class Cat extends Animal {
public int b = 4;
public static int A = 4;
public static void Method () {
System.out.println ("Cat");
}
public void Voice () {
System.out.println ("Cat bark");
}
}
Class Animal {
public int b = 0;
public static int A = 0;
public static void Method () {
System.out.println ("Animal");
}
public void Voice () {
System.out.println ("animal called");
}
}
The output results are:
0
0
Cat Bark
Animals
As you can see, when the parent class animal reference A to the subclass dog, Rtti automatically determines the true type of the reference at runtime, and when the subclass overrides the parent class's method, it directly invokes the subclass's method, printing out the "Cat's name", whereas a non-static method overridden in a subclass is overwritten. And the static method of the quilt class rewrite words is hidden, in addition, static variables and member variables are also hidden, and Rtti is only for overlays, not for shadows, so static variables A and non-static variable B and static method methods () are not passed through the RTTI, which is the reference to which class is called the static method, member variables, and here is a reference to the parent class animal, so you call the methods in the parent class animal and the member variables directly. So static Methods method (), static variable A and member variable B print the results all in the parent class. Print subclasses only with voice () of the overridden Non-static method.