Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Private Members in Inheritance: What Subclasses Actually Get

Understand how private fields, methods, and constructors behave in Java inheritance, including access rules, polymorphism limits, and design implications.

~3 min readJava
#java inheritance#private members#access modifiers#method overriding
Diagram showing a parent class with private fields and a child class that cannot access them directly.

The Real Meaning of Inheritance for Private Members

In Java, the private keyword restricts access to the declaring class only. When a subclass extends a superclass, private members are not inherited in the sense that the subclass cannot access them directly by name. The subclass object still contains the superclass's private fields, and private superclass methods still run when invoked from within the superclass. That distinction is central to how private members behave during inheritance.

Private Fields: Present but Not Accessible

Consider this example:

java
public class Animal { private int age; public Animal(int age) { this.age = age; } public int getAge() { return age; } } public class Dog extends Animal { public Dog(int age) { super(age); } public void describe() { // System.out.println(age); // compile error: age has private access in Animal System.out.println(getAge()); // works } }

The field age is private to Animal. A Dog object has an age field allocated, but Dog code cannot reference age directly. It must use the public getAge() method. This is not a limitation of the language; it is an encapsulation guarantee. The superclass controls how its private state is accessed.

Private Methods: Not Overridable, Not Polymorphic

Private methods are not inherited and cannot be overridden. If a subclass declares a method with the same signature as a private method in the superclass, that method is a new method, not an override. This has important consequences for polymorphism.

java
public class Base { private void display() { System.out.println("Base display"); } public void callDisplay() { display(); } } public class Derived extends Base { private void display() { System.out.println("Derived display"); } } // Usage Base obj = new Derived(); obj.callDisplay(); // prints "Base display"

Even though Derived has its own display(), the call inside callDisplay() is resolved to Base.display() because private methods are bound at compile time, not dynamically. This is a common source of confusion when developers expect polymorphism to apply to private methods.

Accessing Private Members Through Public or Protected Methods

Under normal inheritance rules, a subclass can interact with a superclass's private members only through methods that the superclass exposes. This is a deliberate design pattern. It allows the superclass to maintain invariants, validate inputs, or log access. For example, a setter method can enforce constraints:

java
public class BankAccount { private double balance; public void deposit(double amount) { if (amount > 0) { balance += amount; } } public double getBalance() { return balance; } }

A subclass cannot directly modify balance, so it cannot accidentally corrupt the account state. This is a core benefit of encapsulation.

Private Constructors and Their Effect on Inheritance

A private constructor cannot be called from a subclass. Since every subclass constructor must call a superclass constructor, a class with only private constructors cannot be extended.

java
public class Singleton { private Singleton() {} } // class SubSingleton extends Singleton {} // compile error: Singleton() has private access in Singleton

This is often used to enforce singleton patterns or utility classes. If you need to allow subclassing outside the same package, make an accessible constructor protected or public; a package-private constructor only works for subclasses in the same package.

Common Misconceptions and Debugging Pitfalls

A frequent mistake is assuming that a subclass can access private fields by using super.field. That fails to compile. Another pitfall is declaring a method with the same name as a private method in the superclass and expecting it to be called polymorphically. As shown earlier, it will not be invoked through a superclass reference.

When debugging, remember that private members are not visible in the subclass's scope, but they are present in the object. They are part of the object even though they are outside the subclass's compile-time scope. Reflection can access private members, but that is outside normal inheritance rules.

Design Implications for Maintainability

The behavior of private members in inheritance directly affects how you design class hierarchies. Exposing private state through protected methods can weaken encapsulation, while keeping everything private forces subclasses to rely on the public API. The choice depends on whether subclasses need to customize behavior.

A common pattern is to use protected methods as hooks for subclasses, while keeping fields private. This gives subclasses the ability to modify behavior without exposing the internal state directly. For example:

java
public class Shape { private double area; protected void setArea(double area) { this.area = area; } public double getArea() { return area; } } public class Circle extends Shape { public Circle(double radius) { setArea(Math.PI * radius * radius); } }

Here, Circle uses the protected setter to update the private field, preserving the superclass's control over the field's type and validation.

Understanding these rules helps you avoid subtle bugs and write class hierarchies that are both flexible and safe.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.