I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.
Can someone explain this to me. I am now totally confused.
I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.
Can someone explain this to me. I am now totally confused.
IMO by no way is it a matter of definition. In class-based Inheritance implies propagation of behavior to descendants. As such private members DO get inherited , and I will not go into the details how this happens.
Actually I find the "not inherited" answer to be dangerous for new developers and they do not comprehend right away that the private members are there hidden under the skin of your class and they (can) have severe impact on its behavior, size of the objects etc.
It is common that "development comes before understanding" in computer science, however lets avoid building (or destroying) our conceptualization of OOP assuming the wrong "definition" adopted by some technician writing the manual of a well known class based OO platform.
Sorry for stating something in such an old post, but the issue is always valid.
No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited.
From the
Java Documentation
,From the
JLS
,A useful link : Does subclasses inherit private fields?
You will be satisfied here 100%. I tested it on my computer and what I concluded I'm going to post it here. Just go through the program written below, see the program output and READ THE CONCLUSION given at the end. To test it yourself, copy the whole program and save it in a file named "InheritanceTest.java" then compile it and finally run it.
Program:
This kind of depends on your exact usage of the word inheritance. I'll explain by example.
Suppose you have two classes:
Parent
andChild
, whereChild
extendsParent
. Also,Parent
has a private integer namedvalue
.Now comes the question: does
Child
inherit the privatevalue
? In Java, inheritance is defined in such a way that the answer would be "No". However, in general OOP lingo, there is a slight ambiguity.You could say that it not inherited, because nowhere can
Child
refer explicitly tovalue
. I.e. any code likethis.value
can't be used withinChild
, nor canobj.value
be used from some calling code (obviously).However, in another sense, you could say that
value
is inherited. If you consider that every instance ofChild
is also an instance ofParent
, then that object must contain 'value' as defined inParent
. Even if theChild
class knows nothing about it, a private member namevalue
still exists within each and every instance ofChild
. So in this sense, you could say thatvalue
is inherited inChild
.So without using the word "inheritance", just remember that child classes don't know about private members defined within parent classes. But also remember that those private members still exist within instances of the child class.
Though https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2 shows that Private Members are not inherited. Actually, it is inherited by subclass. When we use debuggers to trace variables, it will show the private members under the label of "inherited", so just try it. there is another post discussing this question, and most of them think not inherited, which misleads many people, including me at first.