0

Why the the code (1) is not result an error while code (2) (3) will?

I think when the subclass calls the constructor, it will calls super class constructor first, but I do not know why the code (1) is right while other two are wrong.

//(1)

public class Parent {
  public int a;

  public Parent() {
    this.a = 0;
  }
}

public class Child extends Parent {
  public Child() {}
}

//(2)

public class Parent {
  public int a;

  public Parent(int number) {
    this.a = number;
  }
}

public class Child extends Parent {
  public Child() {}
}

//(3)

public class Parent {
  public int a;

  public Parent(int number) {
    this.a = number;
  }
}

public class Child extends Parent {
  public Child(int numb) {      
  }
}

Code(1) is right while other two are wrong.

  • What is the error? – John3136 Oct 02 '19 at 01:24
  • `1` is right because it's well-formed Java code. `2` is wrong because the Child's constructor calls a default constructor for Parent, which doesn't exist. The same problem exists in code sample `3`. – Avi Oct 02 '19 at 01:25
  • @Avi why (2)(3) not have default constructor while (1) has? – diiiiiklllllll Oct 02 '19 at 01:27
  • 2
    You defined an actual constructor for the Parent in `2` and `3`. That means that the *default* constructor is replaced by the actual constructor. – Avi Oct 02 '19 at 01:28
  • Possible duplicate of [Calling superclass from a subclass constructor in Java](https://stackoverflow.com/questions/19326229/calling-superclass-from-a-subclass-constructor-in-java) – MAO3J1m0Op Oct 02 '19 at 01:29
  • @Avi But isn't (1) also defined an actual constructor for Parent? – diiiiiklllllll Oct 02 '19 at 01:33
  • 1
    @Cathy Oops, that's true. In that case, you have a constructor that takes no arguments for `1`. However, no such constructor exists for `2` and `3`. I glossed over the constructor because it had no effect different than the default constructor. – Avi Oct 02 '19 at 01:34
  • This is what you need: ``` public class Parent { public int a; public Parent(int number) { this.a = number; } } public class Child extends Parent { public Child(int numb) { super(numb); } } ``` – Avi Oct 02 '19 at 01:37

2 Answers2

3

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

So, here it is, your code (2)(3) doesn’t have a no-argument constructor, and also you didn’t explicitly invoke a has-argument constructor, you got a compile-time error. More details from https://docs.oracle.com/javase/tutorial/java/IandI/super.html

Hugo Zhang
  • 51
  • 3
1

In code 1, the constructor for Parent has no arguments, so a call to the default one is implicit:

public Child () {
    super();
} /* This code is not necessary, but is implied. */

But in codes 2 and 3, the constructor has a parameter, and since there is no overload provided with no parameters, then a call to the superclass constructor must be provided. To do this, you must reference super().

public class Parent {
    public int a;

    public Parent(int number) {
        this.a = number;
    }
}

public class Child extends Parent {
    public Child(int numb) {
        super(numb); // Calls Parent(int) and sets this instance’s Parent.a value to numb.
    }
}
MAO3J1m0Op
  • 423
  • 3
  • 14