Class fu{
int a=2;
Fu () {
System.out.println ("Fu Run");
}
}
Class Zi extends fu{
Zi () {
System.out.println ("Zi Run");
}
}
public class Copy {
public static void Main (string[] args) {
Zi Zi = new Zi ();
System.out.println (ZI.A);
}
}
Run the result is Fu Run, Zi run, 0 description subclasses when instantiating, the method in the constructor of the parent class is called, but the member variable is not invoked. ,
====== Subclass instantiation When the default call to the parent class is empty construction method namely super ();
Can not write but when the parent class does not have an empty constructor, it is necessary to invoke the non-empty constructor method in the subclass to instantiate the parent class before the example class. The null constructor method is automatically called by default in the subclass.
======== when a parent class refers to a subclass object, it calls different classes based on the class that the instance belongs to in the reference variable, which applies only to methods that do not apply to member variables.
The simple understanding that Java does not support attribute field overrides (override) only supports method overrides.
When ====== instantiates a subclass, the constructor of the parent class is initialized, but the object is not produced because the parent class is likely to be an interface and is called by default if the parent class has an empty construction method. Because the subclass must hold the value of the member variable of the parent class, it is initialized and cannot be overridden for member variables, and overrides do not have a polymorphic effect.
Ctrl N New class remember to tick
Constructors from superclassIt's best to make it easy by default. This will add an empty construction method by default.
Constructors that do not have an empty constructor cannot be invisible to initialize the method that the bean reflection needs to be empty.
Constructor method member variable for Java subclass initialization Parent class does not support polymorphism