Course contentsShow
← Java
Lesson 716 of 2,87017. Access Control in DepthFree lesson

Private Members Are Not Inherited

Private fields and methods are not accessible in subclasses; understanding hidden vs inherited members.

Private Members Are Not Inherited

What you'll learn: You'll understand why private fields and methods don't become part of a subclass's inherited members, even though they technically exist in memory.

The Core Idea

When a subclass extends a parent class, it inherits all the parent's accessible members. However, private members are not inherited—they remain locked inside the parent class, invisible and inaccessible to the subclass. This is a critical distinction: the subclass object contains those private members in memory (they're part of the parent portion), but the subclass cannot see or use them directly.

Think of it like a safe inside a house you inherit. You get the house (the parent class), and the safe is physically inside it, but you don't have the combination. The safe's contents (private members) exist, but you can't access them.

Hidden vs. Inherited

  • Inherited members: Fields and methods the subclass can directly access and use (public, protected, package-private if in same package)
  • Hidden members: Private members that exist in the parent portion of the object but remain completely inaccessible to subclass code

The subclass must use the parent's public or protected methods (getters/setters) to interact with private data—it cannot reference those private fields or call those private methods directly.

Why This Matters

This design enforces encapsulation across inheritance hierarchies. The parent class maintains full control over its internal implementation. Even if you extend a class, you cannot break into its private implementation details.

Key Takeaway: Private members are not inherited—subclasses cannot access them directly, even though those members exist in memory as part of the parent object structure. Only public, protected, and package-private members (in the same package) are truly inherited and accessible to subclasses.