Sunday, 5 June 2016

java - Why is super class constructor always called

I have the following 2 classes



public class classA {

classA() {
System.out.println("A");
}
}

class classB extends classA {
classB() {
System.out.println("B");
}
}



and then running





classA c = new classB();


or






classB c = new classB(); 


always gives



A
B



Why is this happening? At first glance, in either scenario, I would assume that only the classB constructor would be called and thus the only output would be



B


but this is clearly wrong.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...