Is not the sub class inheriting all of the members from the super class regardless, so why not be able to override the private member variables from the super class? Would it not be safe to assume that the sub class has its own private member version and be able to override it in Java?
-
4If the variables are private it means that are only visible from the class in which they are defined – Josep Jul 09 '20 at 16:12
-
@Josep but would not the sub class inherit them still have them as it’s own members? – VividMan Jul 09 '20 at 16:12
-
Subclasses do not inherit private members. – Joni Jul 09 '20 at 16:16
-
2If you want the parent class's attributes to be accessible in a child class you can use `protected` keyword before those attributes – Salavat Yalalov Jul 09 '20 at 16:16
-
No, by design https://stackoverflow.com/questions/6543328/private-members-in-java-inheritance/10355759 – PeterMmm Jul 09 '20 at 16:17
-
@PeterMmm the answer you linked does say that they do get inherited but cannot be accessed? – VividMan Jul 09 '20 at 16:22
-
https://stackoverflow.com/questions/7794621/hiding-instance-variables-of-a-class – daniu Jul 09 '20 at 16:26
-
2"Private" means "not anybody else's business". As far as anyone else is concerned, private members do not exist, and the class can change the organization at any time. – chrylis -cautiouslyoptimistic- Jul 09 '20 at 16:35
-
The subclass does inherit the private members, but it can only access them if the super class has protected or public methods to change the private members. – NomadMaker Jul 09 '20 at 17:07
3 Answers
Subclasses do not inherit private members in java. Subclass doesn't know all thing about parent class

- 1,019
- 9
- 13
Would it not be safe to assume that the sub class has its own private member version and be able to override in Java?
NO
Just think to write a library and not just code for only your current project.
Your classes can be inherited to add more functionality, but the user of your library should not change your behavior.
A simple example could be the AtomicInteger
Java class, it has private volatile int value;
field.
Now, if you could access at that member just inheriting the class, would mean that you could change the value loosing all AtomicInteger
thread safety.
I'm sure that if you look in java.util
package you will find a lot of similar cases,
in which some field is and must be managed internally and accessed only from methods

- 475
- 4
- 17
You've gotten some hints from the other info, but here's an example. Imagine two classes, Foo and Bar. Bar inherits from Foo. Here's a simple implementation of each:
public class Foo {
private void myPrivateMethod() { System.out.printf("myPrivateMethod()\n"); }
public void myPublicMethod() { System.out.printf("myPublicMethod()\n"); }
}
public class Bar extends Foo {
public void barPublicMethod() {
System.out.printf("This is barPublicMethod()!\n");
myPublicMethod();
myPrivateMethod();
}
public static void main(String[] args) {
System.out.printf("This is main!\n");
Bar bar = new Bar();
bar.barPublicMethod();
}
}
If you try to compile this code, you get this error:
$ javac Foo.java Bar.java && java Bar
Bar.java:6: error: cannot find symbol
myPrivateMethod();
^
symbol: method myPrivateMethod()
location: class Bar
1 error
If you remove the call to myPrivateMethod(), it works. This is the nature of private methods.
Bar knows NOTHING about the private methods of Foo. NOTHING. Which means you can't override them, because Bar doesn't even know they exist. That's what private means.
Bar can't do one single thing with private methods from Foo. Nothing. If you want Bar to be able to override them, you need to change the definition in Foo to protected or public.

- 8,530
- 1
- 19
- 36