0

Consider there are three classes A, B and C. Class A doesn't have a constructor, ClassB has a constructor and Class C has a parameterized constructor. something like the example given below.


public class ClassA {

}

public class ClassB extends ClassA {

    public ClassB() {
        System.out.println("Default cons Class B");
    }

}

public class ClassC extends ClassB {

    public ClassC(int a, int b) {
        System.out.println("This is class C "+a+ "and"+ b );

    }
    public static void main(String args[]) {
        ClassC c = new ClassC(2,3);
    }
}

Output:

Default cons Class C

This is class C 2and3

Question 1:

To construct object C it constructs B and to construct B it constructs A first. Even though there isn't any default constructor in class A defined, the program works fine by construction its own default constructor and the B class calls super(). I don't have an issue here however when I change the Class B something like this

public class ClassB extends ClassA {

    public ClassB(int a, int b) {
        System.out.println("This is class C "+a+ "and"+ b );
    }   
}

I am getting an error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Implicit super constructor ClassB() is undefined. Must explicitly invoke another constructor

    at ClassC.<init>(ClassC.java:4)
    at ClassC.main(ClassC.java:10)

Why do we have to implicitly specify the super constructor ClassB(), It worked fine for the first example even though there isn't any super constructor in ClassA(). I am wondering if by default the C's constructor by default calls B's unspecified constructor just like it did for Class A.

1
  • A default constructor does not need to be defined, as long as you don't define another constructor, it will contain public ClassA() Commented Jan 26, 2016 at 20:29

2 Answers 2

4

If you specify at least one constructor in a class a default constructor is no longer created for you.

If you do not specify which parent constructor to call in your subclass the one that takes 0 parameters is used by default.

Since you added a constructor to class B that takes two integer parameters you now have to call it like this from in C as there is no longer a constructor that takes no parameters in class B:

public ClassC(int a, int b) {
    super(a, b);
    System.out.println("This is class C "+a+ "and"+ b );
}
Sign up to request clarification or add additional context in comments.

Comments

1

Because to create an instance of C, the JVM calls the constructors of the class hierarchy.

So when you create C, you actually first call the constructor of A, then of B, then of C. Implicitly, super() is called if nothing else is specified.

If you create a constructor that takes parameters explicitly, then that overrides the default constructor

public B() {
    //default constructor
}

Which means Java won't be able to invoke it for you implicitly as it does not exist, so you have to call it yourself with

public C(int a, int b) {
    super(a, b);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.