Home > Java > javaTutorial > body text

The constructor method under Java inheritance relationship calls the implementation method

黄舟
Release: 2017-10-14 09:36:32
Original
1355 people have browsed it

This article mainly introduces the relevant information on the detailed explanation of constructor method calls under Java inheritance relationship. I hope this article can help everyone understand and master this part of the content. Friends in need can refer to it

Detailed explanation of the constructor call under Java inheritance relationship

When creating an object of a class in Java, if the class has a parent class, the constructor method of the parent class is called first, and then called Subclass constructor. If the parent class does not define a constructor, the default constructor without parameters automatically created by the compiler is called. If the parent class defines a public parameterless constructor, the parameterless constructor will be automatically called before calling the child class's constructor. If the parent class only has a parameterized constructor and no parameterless constructor, the subclass must explicitly call super (parameter list) in the constructor to specify a parameterized constructor. If the parent class defines a parameterless constructor, but the parameterless constructor is declared as private, the subclass must also explicitly call super (parameter list) in the constructor to specify a parameterized constructor. If the parent class has no other parameterized constructor, the subclass cannot be created.

##SubclassPublic no-parameter constructorPrivate no-parameter constructorPublic no-parameter constructorNoneNoneNoneAll constructors will call the default constructor of the parent classYesNoneNoneAll constructors will call the defined parameterless constructorNoneNoneYesAll constructors must specify to call a constructor with parameters, or call some other constructor through this. Yes No Yes You can specify to call a certain constructor. If not specified, the no-argument constructor will be called. method. NoneYesNoneSubclass cannot be constructed (parent class cannot derive subclass)NoneYesYesAll constructors must specify to call a constructor with parameters, or call some other constructor through this construction method.
There is a parent class


For example:


class Parent {
  private String pString;
  Parent(){
    pString = "p1";
  }
}

class Child extends Parent {
  private String cString;
  Child() {
    cString = "c1";
  }
}
Copy after login

When the Child object is created, the constructor of the parent class Parent will first be called to convert pString Initialize it to "p1", and then call Child's constructor to initialize cString to "c1".


class Parent {
  private String pString;
  private Parent(){
    pString = "p1";
  }
  Parent(String s){
    pString = "p2";
  }
}

class Child extends Parent {
  private String cString;
  Child() {
    super("");
    cString = "c1";
  }
}
Copy after login

Since the parent class defines a private parameterless constructor, you must explicitly specify to call a parameterized constructor in the subclass.

The above is the detailed content of The constructor method under Java inheritance relationship calls the implementation method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template