Constructor method member variable for Java subclass initialization Parent class does not support polymorphism

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.