For the constructor invocation and invocation order in the inheritance, a small test was made, as follows:
CLASS PEOPLE { STRING NAME;  //2, step two, call this parameterless constructor public people () { system.out.println (", parameterless constructor of the parent class");  }  //5, call this parameter constructor public people (String name) { system.out.println ("The constructor of the parent class"); this.name = name; }} Class child extends people { people father; public child () { system.out.println (", non-parametric constructor for subclasses"); } //3, creating the parameter "Mike" that passed the history, so the third step calls the subclass with the parameter constructor Public child (String name) { system.out.println (", the subclass of a parametric constructor"); this.name  = NAME;    //4, where an instance of people is created and arguments are passed, the parent class is called with a parameter constructor father = New people (name + ": F"); } public static void main (String[] args) { system.out.println ("Constructor call order when a subclass creates an instance:"); new child ("Mike");//1, creating a subclass instance, Because the child class does not display a constructor that calls people, the defaultCall People's parameterless constructor // new child ();//Do not pass arguments, the first step calls the parent class without the parameter constructor, and then calls its own parameterless constructor. }}
The current state executes the main method and outputs the following:
The order in which constructors are called when the child class creates an instance:
, no parameter constructor for the parent class
, the sub-class of a parameter constructor
, a parameter constructor for the parent class
If the subclass does not show the call to the parent class constructor, when the child class creates an instance, it calls the parent class's parameterless constructor, and if the parent class does not have a parameterless constructor, an error is given.
This article is from the "Moon Pine Room Photo" blog, please be sure to keep this source http://javawutong.blog.51cto.com/12881544/1922764
Constructor calls in Java inheritance