Constructor calls in Java inheritance

Source: Internet
Author: User

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

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.