Are private members in a superclass members of a subclass?
Are private members of a superclass also members of a derived subclass?
Obviously the subclass cannot access the associated superclass's private
members.
If the private members of the superclass are also members of the subclass
then they are inherited from the superclass.
In C++ even though private members of a base class are members of the derived
class, they are not accessible to the members or friends of the derived class.
Does this hold true with Java as well?
Re: Are private members in a superclass members of a subclass?
"David Reeves" <dnreeves@swbell.net> wrote:
>
>Are private members of a superclass also members of a derived subclass?
Yes they are. And they have to:
Imagine you use in your derived class a method of your super class, which
in turn uses the private field. If the field would be optimized away from
the object layout of your class the method would fail.
But you're right. With a deeper analysis the compiler can find cases where
the field may be optimized away (personally I think this would be not worth
the effort. Hmmm? Perhaps in embbeded environments with very sparse resources?).
Regards
Thomas
Re: Are private members in a superclass members of a subclass?
"ThomasW" <Th.Weinstein@xcc.de> wrote:
>
>"David Reeves" <dnreeves@swbell.net> wrote:
>>
>>Are private members of a superclass also members of a derived subclass?
>
>Yes they are. And they have to:
>
>Imagine you use in your derived class a method of your super class, which
>in turn uses the private field. If the field would be optimized away from
>the object layout of your class the method would fail.
>
>But you're right. With a deeper analysis the compiler can find cases where
>the field may be optimized away (personally I think this would be not worth
>the effort. Hmmm? Perhaps in embbeded environments with very sparse resources?).
>
>
>Regards
>
>Thomas
>
>
The JLS states:
"Members of a class that are declared private are not inherited by subclasses
of that class. Only members of a class that are declared protected or public
are inherited by subclasses declared in a package other than the one in which
the class is declared"
So - how can a private member of a superclass that is not inherited by a
subclass be a member of the subclass?
Re: Are private members in a superclass members of a subclass?
From the Java Language Specification:
"The members of a class type (§8.2) are fields and methods. The members of a
class type are all of the following:
Members inherited from its direct superclass (§8.1.3), if it has one
(the class Object has no direct superclass)
Members inherited from any direct superinterfaces (§8.1.4)
Members declared in the body of the class (§8.1.5) "
Since private members are not inherited, it follows from this that they are
not members of the subclass.
PC2
David Reeves <dnreeves@swbell.net> wrote in message
news:39f362ce$1@news.devx.com...
>
> Are private members of a superclass also members of a derived subclass?
>
> Obviously the subclass cannot access the associated superclass's private
> members.
>
> If the private members of the superclass are also members of the subclass
> then they are inherited from the superclass.
>
> In C++ even though private members of a base class are members of the
derived
> class, they are not accessible to the members or friends of the derived
class.
>
> Does this hold true with Java as well?
>
Re: Are private members in a superclass members of a subclass?
"David Reeves" <dnreeves@swbell.net> wrote:
>The JLS states:
>
>"Members of a class that are declared private are not inherited by subclasses
>of that class. Only members of a class that are declared protected or public
>are inherited by subclasses declared in a package other than the one in
which
>the class is declared"
>
>So - how can a private member of a superclass that is not inherited by a
>subclass be a member of the subclass?
>
>
I think the JLS means "inherited" in a logical sense: The private field is
NOT visible/accessible for methods in the derived class, but in a "physical"
sense you will need the field in the memory layout of objects of your derived
class for the methods of the super class to work.
Thomas