When a new object is created, all inherited constructors are inherited
- This means that each parent class has a constructor (because each class has at least one constructor), and each constructor executes when the object is created.
- Executing The new instruction is a big event because he will start a chain reaction to the constructor. Also, even abstract classes have constructors. Although you cannot perform a new operation on an abstract function, the abstract function is also the parent class, so its constructor is executed when the instance is created by a specific subclass.
- Call the constructor portion of the parent class with super in the constructor (note: Calling the super () method is the only way to call the parent class constructor). Remember that subclasses inherit methods based on the state of the parent class (that is, the instance variables of the parent class )
Example
supplement : As shown in the example, hippo objects Is-a Animal also is-a Object , if you need to create hippo , you also need to create a animal and object part of , such a process is called " constructor chain (constructor Chaining )".
1 Public class Animal {2 Public Animal () {3 System.out.println ("Maka a Animal"); 4 }5 }
1 Public class extends animal{2 Public Hippo () {3 // super (); Add by default, call parent class 4 System.out.println ("Make a Hippo"); 5 }6 }
1 Public classTesthippo {2 3 Public Static voidMain (string[] args) {4 //TODO auto-generated Method Stub5System.out.println ("Starting ...");6Hippo hippo=NewHippo ();7 }8 9}
Sequential Execution
- First, the program executes Action for New Hippo () , hippo () 's constructor goes into the stack block at the top of the stack
- hippo () calls the constructor of the parent class to cause animal () The constructor of the enters the top of the stack
- animal () calls the constructor of the parent class to cause object () constructor enters top of stack
- object () completes, its stack is quickly ejected, and then continues to execute Animal ()
- and so on ...
Why do subclasses have to access constructors in the parent class?
- Because the subclass of data in the parent class can be obtained directly, the sub-class caprice, when established, needs to look at how the parent class initializes the data, so that when the object is initialized, the child class first accesses the constructor in the parent class
- If you want to access the constructor specified by the Frey, you can specify the call by manually defining the Super statement
Plus Example:
1 classfu{2 intX =30;3 Fu ()4 { 6 System.out.println (x);7 }8Fu (intx)9 {Ten System.out.println (x); One } A } - classZiextendsFu { - Zi () { the //Seper (); - //Super (4); //specifies access to a constructor with a parameter in the parent class - This(20); -System.out.println ("Zi ..." +x); + } -Zi (intx) + { ASystem.out.println ("Zi ..." +x); at } - - } - Public classSingle { - Public Static voidMain (string[] args) { -Zi z=NewZi (); in //Zi z1=new Zi (3); - } to}
Execution Results :
Execution order:
- constructor to access the sub-class hollow parameter first
- When executing a subclass constructor, because of the existence of an implicit super () , the empty constructor of the parent class is executed first, and the output
- Next executes this (), because it points to a constructor with one parameter in this class, so System.out.println ("Zi ..." +x) with one parameter in the execution subclass
- Finally executes the System.out.println ("Zi ..." +x)of the next line (note: where X is inherited from the parent class)
The role that the parent class's constructor plays in the life of the object