Explicit constructor still has default values even though a default constructor is not invoked.

Posted by Phoenix on Stack Overflow See other posts from Stack Overflow or by Phoenix
Published on 2012-08-29T03:12:36Z Indexed on 2012/08/29 3:38 UTC
Read the original article Hit count: 148

Filed under:

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 ?

© Stack Overflow or respective owner

Related posts about java