Whether the Java constructor can be inherited, and if the subclass constructor can call the superclass constructor without using super

Source: Internet
Author: User

Question one: Can Java constructors be inherited?

The author of Java read a book said: "Java subclasses naturally inherit its superclass" non-private members.

Normally Java constructors are set to public (if you do not write the constructor, Java automatically adds the parameterless null constructor is public), because the classes in this article are in the same package, so the use of non-decorated friendly permissions to explain the problem, for the meaning of the private constructor, can be found here.

So according to the rules of the book, non-private constructors of course should also inherit the quilt class.

In practice, however, a subclass cannot inherit a private member, nor can it inherit a constructor.

This is illustrated in the code below.

Example 1.

The system automatically adds an empty constructor: (if the constructor is not written, Java adds an empty constructor without parameters by default)

[Java]View Plaincopy
    1. Class base{
    2. }
    3. Class derived extends base{
    4. public static void Main (string[] args) {
    5. Derived d=new derived ();
    6. }
    7. }

Example 2.

The non-parametric null constructor that the system automatically adds in the parent class is explicitly written out (in order to have the output, add println):

[Java]View Plaincopy
    1. Class base{
    2. Base () {
    3. System.out.println ("base constructor");
    4. }
    5. }
    6. Class derived extends base{
    7. public static void Main (string[] args) {
    8. Derived d=new derived ();
    9. }
    10. }
Output: base constructor, code compiled. From the example, it seems that subclasses can inherit the superclass constructor.

But the reality is: Java rules: When a subclass creates an object, it first creates an object of the parent class, so the constructor of the parent class must be called first. In Example 2, derived does not override the constructor method, according to the Java rules, the system will default to add a non-parametric construction method, and the first sentence of the method is super (). The explicit write-up is:

[Java]View Plaincopy
    1. Derived () {
    2. super ();
    3. }

That is why subclasses can not write, because: if the parent class "only" has no parameter construction method, and does not intend to rewrite the subclass of the construction method, in order to save the code, the subclass construction method can not write, the system calls the parent class without the parameter construction method super () by default.

Perhaps some people think that these examples are not persuasive enough, still think that the construction method can inherit, it's okay, after reading this article, especially example 4 with the last with ★ text, can fully explain the problem.

Let's take a look at the method of constructing a parameter:

Example 3.

If you change the constructor method of the superclass to a method with the constructor, the code is shown below, then eclipse will report "Implicit super constructor xxx is undefined for default constructor. Must define an explicit constructor "

Description The subclass is not inheritable for the method of constructing the superclass.

[Java]View Plaincopy
    1. Class base{
    2. Base (int x) {
    3. System.out.println ("base constructor,i=" +x);
    4. }
    5. }
    6. Class derived extends base{
    7. public static void Main (string[] args) {
    8. Derived d=new derived ();
    9. }
    10. }

For a constructor that has parameters, you must define your own constructor method in the subclass.

(Note that parameter matching, the first sentence of the super (Paramlist) to the parent class constructor method, if the multi-level inheritance, you need to pass the parameter to the topmost layer of the method of the parameter)

[Java]View Plaincopy
  1. Class base{
  2. Base (int x) {
  3. System.out.println ("base constructor,i=" +x);
  4. }
  5. }
  6. Class derived extends base{
  7. Derived (int x) {
  8. super (x);  //The Super (param) is used here, and the construction method of the superclass is called.
  9. System.out.println ("derived constructor,i=" +x);
  10. }
  11. public static void Main (string[] args) {
  12. Derived d=new derived (8);
  13. }
  14. }
Output Result:

Base Constructor,i=8

Derived constructor,i=8

Question two: Can the constructor of a subclass not use super (paramlist) to call the constructor of a superclass?

The above example uses super to call the constructor of the superclass. The same book says, "the constructor of a subclass must use super to call the constructor of the superclass, and super must be the first sentence of the subclass construction method." ”

So I don't need a super, okay? The exact words are like Java syntax.

In fact, you can not explicitly write super, but the premise is "there are multiple construction methods in the superclass, and there is an explicit write-out of the non-parametric construction method."

Example 4.

A superclass has multiple construction methods, one of which is an explicit, non-parametric construction method.

[Java]View Plaincopy
  1. Class Base {
  2. Base () { //warning_su
  3. System.out.println ("base constructor."); //warning_su
  4. } //warning_su
  5. Base (int x) {
  6. System.out.println ("Base constructor,i=" + x);
  7. }
  8. }
  9. Class derived extends base {
  10. Derived () { //warning_de
  11. System.out.println ("derived constructor"); //warning_de
  12. } //warning_de
  13. Derived (int x) {
  14. //super (x);  Super is commented out!
  15. System.out.println ("derived constructor,i=" + x);
  16. }
  17. public static void Main (string[] args) {
  18. Derived d= new derived (); //WARNING_CR
  19. Derived t = new Derived (8);
  20. }
  21. }
Output Result:

Base constructor.

Derived constructor

Base constructor. <---------note there is no i=8 here.

Derived constructor,i=8

At this point, the subclass of the constructor method that does not use super (X) to call the superclass is also compiled, but does not use Super (paramlist), new derived (8) called the superclass of the non-parametric construction method base ()!

Why emphasize the need to "explicitly write out" the parameterless construction method, you can try to comment out the code with warnning words (WARNING_SU,WARNING_DE,WARING_CR3), but also commented out super (x).

Because the superclass and subclass have a construction method (the one with the parameter), the system does not automatically add an empty constructor with no parameters to the superclass, which will cause the subclass to not find the non-parametric construction method of the superclass super () to use, also reported "implicit super constructor XXX is Undefined for default constructor. Must define an explicit constructor ".

In fact, even if we do not write super (), the system will automatically add a super (), in this case super () does not exist so the error.

Of course, subclasses do not call the superclass of the parameter constructor super (Paramlist) is relatively few cases, so the book simply must be written in the first sentence of super (paramlist).

★ In addition, if you comment out the parameterless constructor derived () in the subclass (without commenting on Warning_su and Warning_de), Eclipse will say: "The constructor derived () is undefined".

It is also proved that even non-parametric constructors cannot be inherited. In order to save effort when there is only one parameterless constructor in the superclass and no intention to rewrite the subclass constructor, the subclass constructor may not write, and Java will automatically add a super () to run when the constructor of the subclass is used.

Whether the Java constructor can be inherited, and if the subclass constructor can call the superclass constructor without using super

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.