Subclass inherits Parent class, constructor features super () __ function

Source: Internet
Author: User

When initializing a subclass, the constructor of the parent class must run

Because there is an implicit statement super () in the first row of the constructor of the subclass, all constructors of the subclass default to the first row of super ();

Subclass calls the parent class constructor Super (parameter), the subclass calls the parent class general method: Super. Method name

Subclasses must access the parent class.

Because the parent class's data subclass can be obtained directly, when the subclass object is established, you need to see how the parent class initializes the data.

1. When the constructor of a null parameter is in the parent class, the first line in all the constituent functions of a subclass has a default of SUPR (): Calling the null parameter of the parent class to form a function; So subclass initialization, you first access the parent class constructor, and if you want to access the constructor specified by the parent class, you can manually define the super statement. At least one constructor in a subclass accesses the parent class constructor


Class Fu {
Fu ()//when the constituent function of the parent class is an empty parameter
{
System.out.println ("Fu Run" +x);
}
}
Class Zi extends Fu
{
Zi ()
{
Super (); constructor with an implicit parent class NULL parameter
System.out.println ("Zi Run");//When the object calls this method, first call the parent class null parameter constructor, output Fu run, in the output of this output statement
}
Zi (int x)
{
Super (); constructor with an implicit parent class NULL parameter
System.out.println ("x=" +x);//When the object calls this method, first call the parent class null parameter constructor, output Fu run, in the output of this output statement
}


}
Class Extendsdemo
{
public static void Main (string[] args)
{

Zi z=new Zi ();
Zi z1=new Zi (3);
}

}

2. When the constructor of the null parameter is not in the parent class, it is the parent class constructor of the parameter. How super () cannot be implicit. You must add super by default on the first line of the constituent function of the subclass (parameter

Class Fu
{
Fu (int x)
{
System.out.println ("Fu Run");
}
}
Class Zi extends Fu
{
Zi ()
{  
Super (3);//This line has an implicit parent class null parameter constructor super ();
System.out.println ("Zi Run");//When the object invokes this method, first invoke the parent class null parameter constructor, output fu run, in output this output statement
}
Zi (int x)
{
Super (4);// When the parent class has no empty parameter constructors, you must receive the add super, access the parent class constructor, super () cannot be implicitly
System.out.println ("x=" +x);//When the object calls this method, the parent class null parameter constructor is invoked to output the FU Run, in the output of this output statement
}


}
Class Extendsdemo
{
public static void Main (string[] args)  
{  &NBSP

Zi z=new Zi ();
Zi z1=new Zi (3);
}

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.