According to my understanding a default constructor initializes the state of the object to default values, so if i provide an explicit no-arg public constructor like this then how are the values of d and e still getting initialized to zero because in this case the default constructor is not invoked.
public class B extends A{
private int d;
private int e;
public B() {
System.out.println(d);
System.out.println(e);
}
}
EDIT:: The only thing default constructor does is call to super() then how come if i have a explicitly mentioned a constructor here and A has a protected variable say c which is initialized to 17 in its constructor. Should I not be explicitly calling super() to be able to see that change since I’m using my own constructor ? Why is B still getting the value of 17 through inheritance ?
All class fields get assigned default values if you don’t explicitly initialize them on declaration, in a initializer block, or in your constructor. Objects get initialized to null, ints to 0, booleans to false, doubles to 0.0, float to 0.0f, long to 0L, char to ‘\u0000`…
Please see the JLS, section 4.12.5. Initial Values of Variables as it explains it all.
Note that these rules do not apply to variables that are local to any block or method, but instead local variables must be initialized explicitly by the coder before use.
Edit
Regarding your edit:
Answer: The
super()default constructor is being called at the very beginning of B’s constructor, whether you explicitly call it or not. The only extra benefit obtained by explicitly calling the super constructor here does is to allow you to call a non-default constructor for A, if one exists, and if you so desire.