Java Neutron classes invoke the constructor method of the parent class

Source: Internet
Author: User
Tags class definition

In Java, in the construction of a subclass, the constructor of its parent class must be called, because the subclass inherits the contents of the parent class and does so by what means.

The answer is as follows:

When you new a subclass object, you must first need to new a parent class of the image out, the parent object is within the subclass object, so that the subclass object is larger than the parent object, the subclass object contains a parent class object, This is the real situation in memory. The construction method is a new object, you must adjust the method, this is the rule, to the new parent object out, then definitely call its construction method, so:

First Rule :

During the construction of a subclass, you must call the constructor method of its parent class. A class, if we don't write the construction method, then the compiler will help us add a default construction method, the so-called default construction method, is no parameters of the construction method, but if you write the construction method, then the compiler will not be added to you, so sometimes when you are new to a subclass object, Must have called the constructor method of the subclass, but in the subclass construct method We do not show the constructor method of invoking the base class, which is not written, such as: super (); This is not written, but it will call the parent class without the parameters of the constructor, if there is no constructor in the parent class without parameters will error.

Second Rule :

If the constructor method of the subclass does not display the calling base class constructor method, then the system defaults to the constructor method of the base class without parameters Note: If the constructor method of the subclass does not show the call base class constructor, and there is no default parameterless construction method in the base class, then the compilation is wrong, so we usually need to display the following: Super (argument list) to invoke the constructor with parameters of the parent class.

The sample code is as follows:

When you do not use the default constructor method for the parent class, you need to display the constructor method that invokes the parent class definition in the constructor method of the subclass.
 class animal{
   private String name;

   If you define a new construction method public
   Animal (String name) {
     this.name = name;
   }
 }

 Public Dog extends animal{

   //At this point you will display the constructor method of the calling parent class, because the subclass defaults to call the parent class's
   //parameterless constructor Animal () public
   Dog () {
     Super ("Puppy");  Shows the parameter constructor method that invokes the parent class ...

     .  Constructor for subclasses
   }

 ////If you are, of course, in the parent class, the parameterless construction method is written out, for example:
 class animal{
   private String name;

   The parameterless construction method public
   Animal () {
     ...  Processing
   }/

   *
   If you define a new construction method, in the constructor method of the subclass, you can not display the call to the parent class, because the subclass has a parameterless construction method
   . Subclasses automatically invoke the parameterless construction method that the parent class has already defined in the constructor method.
   *
   /Public Animal (String name) {
     this.name = name;
   }
 }

Summary: In general, the overload of the constructor method is used in the parent class, and the corresponding parent class construction method can be invoked as needed in the subclass. Therefore, it is recommended that you overload the constructor method in the parent class, which allows you to choose the child classes as needed to improve programming flexibility.

We often say that the members of a class refer to the general designation of the instance variables and methods of the class. The subclass inherits the members of the parent class.

Related Article

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.