Are constructors and private members inherited in Java?
Constructors are never inherited; a child must define its own and call the parent's with super(). Private members exist inside the child object but cannot be accessed by name from the child.
Explain it simply
Think of a family locker. The child gets the contents of the house, but the parent's personal diary (private) stays locked; the child lives with it but cannot read it. And the parent's birth certificate (constructor) is for the parent only, so each child needs their own.
Example
class Parent {
private int secret = 7;
Parent(int x) { }
int reveal() { return secret; } // public path to the private field
}
class Child extends Parent {
Child() { super(1); } // must call a parent constructor
int tryIt() {
// return secret; // compile error: private
return reveal(); // fine
}
}
System.out.println(new Child().tryIt()); // prints: 7The secret field is physically part of every Child object, but only Parent code may name it.
What to say in the interview
"Constructors are not inherited; each class defines its own and chains to the parent with super(). Private fields are part of the child's object but not accessible directly, so the child uses protected or public methods of the parent."
Common mistakes
- Saying private members are not part of the child object. They are stored there, just not visible by name.