Can constructors in the parent class of Java be inherited by subclasses?

The subclass inherits the properties and methods of the parent class by default, but does not inherit the constructor of the parent class. Instead, when the subclass is instantiated, it will call the empty constructor of the parent class by default. The subclass will call its own empty constructor when it is created, and the empty constructor will implicitly call super (), that is, the empty constructor of the parent class. If the constructor of the parent class is overloaded, the constructor in the child class must also initialize the constructor of the parent class, otherwise a compilation error will be reported. Of course, as long as the null constructor is displayed and defined in the parent class, it is not necessary to initialize the constructor of the parent class in the child class. For example, the parent class person java

Subclass: student java

Test. java

Output:

tom

jack

Note: at this time, there is no empty constructor defined in the parent class, only one overloaded constructor. In the child class constructor, the constructor of the parent class must be initialized, that is, super keyword + parameters in the parent class constructor.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>