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);
}