If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

Inheritance of Class with Private Memebers?

I am taking a SmartCertify Direct Java2 course. In its Packages and Inheritance
module it states:

"When you create a subclass, it cannot inherit any of the superclass's
methods and variables that are restricted by access modifiers."

My understanding had been that the access method private would not prevent
inheritance - but would prevent access (overloading/overriding) by a subclass.
• A subclass inherits all the methods and attributes of its superclass(es)
- visibility modifiers affect this: private methods & attributes can not
be accessed in a sub-class. Use protected to allow subclass access

Re: Inheritance of Class with Private Memebers?

Quote from "The Java Language Specification" by Gosling, Joy, and Steele:

"A private class member or constructor is accessible only within the class
body in which the member is declared and is not inherited by subclasses."

There is no restriction on creating a method in a subclass that **appears**
to override a private method of a subclass, i.e. with the same name and
parameter types. However, such a method will not have the polymorphic
behaviour of inherited methods. Here's an example:

In this example, "varA.method1()" will print "I am B" because method1() is
inherited. But "varA.method2()" will print "I am A" because method2() is
not inherited. In effect, B's method2() overloads rather than overrides
A's.

PC2

David Reeves <dnreeves@swbell.net> wrote in message
news:39f34937$1@news.devx.com...
>
> I am taking a SmartCertify Direct Java2 course. In its Packages and
Inheritance
> module it states:
>
>
> "When you create a subclass, it cannot inherit any of the
superclass's
> methods and variables that are restricted by access modifiers."
>
>
> My understanding had been that the access method private would not prevent
> inheritance - but would prevent access (overloading/overriding) by a
subclass.
> . A subclass inherits all the methods and attributes of its superclass(es)
> - visibility modifiers affect this: private methods & attributes can not
> be accessed in a sub-class. Use protected to allow subclass access
>
>
>
> Please elucidate my confusion over this issue.
>
> Thanks
>
> David Reeves
>
>
>
>
>

Re: Inheritance of Class with Private Memebers?

Thanks Paul - but I guess I am still a little confused.

What if a public method X in a superclass depends on an associated private
method Y (in the same class). Suppose a subclass extends the superclass
and accesses the X method.
Since the private members are not inherited - what would be the result?
I understand that the direct access of method Y would be restricted.

"Paul Clapham" <pclapham@core-mark.com> wrote:
>Quote from "The Java Language Specification" by Gosling, Joy, and Steele:
>
>"A private class member or constructor is accessible only within the class
>body in which the member is declared and is not inherited by subclasses."
>
>There is no restriction on creating a method in a subclass that **appears**
>to override a private method of a subclass, i.e. with the same name and
>parameter types. However, such a method will not have the polymorphic
>behaviour of inherited methods. Here's an example:
>
>public class A {
> public void method1() {System.out.println("I am A");}
> private void method2() {System.out.println("I am A");}
>}
>
>public class B extends A {
> public void method1() {System.out.println("I am B");}
> public void method2() {System.out.println("I am B");}
>}
>
>B varB = new B();
>A varA = (A)varB;
>
>In this example, "varA.method1()" will print "I am B" because method1()
is
>inherited. But "varA.method2()" will print "I am A" because method2() is
>not inherited. In effect, B's method2() overloads rather than overrides
>A's.
>
>PC2
>
>David Reeves <dnreeves@swbell.net> wrote in message
>news:39f34937$1@news.devx.com...
>>
>> I am taking a SmartCertify Direct Java2 course. In its Packages and
>Inheritance
>> module it states:
>>
>>
>> "When you create a subclass, it cannot inherit any of the
>superclass's
>> methods and variables that are restricted by access modifiers."
>>
>>
>> My understanding had been that the access method private would not prevent
>> inheritance - but would prevent access (overloading/overriding) by a
>subclass.
>> . A subclass inherits all the methods and attributes of its superclass(es)
>> - visibility modifiers affect this: private methods & attributes can not
>> be accessed in a sub-class. Use protected to allow subclass access
>>
>>
>>
>> Please elucidate my confusion over this issue.
>>
>> Thanks
>>
>> David Reeves
>>
>>
>>
>>
>>
>
>

Re: Inheritance of Class with Private Memebers?

Roughly speaking, the private method Y is only visible to other code in the
same class. Since the code of X is in the same class, it can call Y. This
applies even if X is used by an object in a subclass. (The Java language
specification uses more obscure language to describe this.)

However, if you override X in your subclass, that overriding method cannot
call Y, because its code is not in the same class as Y. However, it can
call super.X(), which then may call Y.

PC2

David Reeves <dnreeves@swbell.net> wrote in message
news:39f350e5$1@news.devx.com...
>
> Thanks Paul - but I guess I am still a little confused.
>
> What if a public method X in a superclass depends on an associated private
> method Y (in the same class). Suppose a subclass extends the superclass
> and accesses the X method.
> Since the private members are not inherited - what would be the result?
> I understand that the direct access of method Y would be restricted.
>

Re: Inheritance of Class with Private Memebers?

"Paul Clapham" <pclapham@core-mark.com> wrote:
>Roughly speaking, the private method Y is only visible to other code in
the
>same class. Since the code of X is in the same class, it can call Y. This
>applies even if X is used by an object in a subclass. (The Java language
>specification uses more obscure language to describe this.)
>
>However, if you override X in your subclass, that overriding method cannot
>call Y, because its code is not in the same class as Y. However, it can
>call super.X(), which then may call Y.
>
>PC2
>
>David Reeves <dnreeves@swbell.net> wrote in message
>news:39f350e5$1@news.devx.com...
>>
>> Thanks Paul - but I guess I am still a little confused.
>>
>> What if a public method X in a superclass depends on an associated private
>> method Y (in the same class). Suppose a subclass extends the superclass
>> and accesses the X method.
>> Since the private members are not inherited - what would be the result?
>> I understand that the direct access of method Y would be restricted.
>>
>
>
>
Paul 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"

I agree with what you are stating but it appears to be inconsistent with
the JLS language.

How can a private member of a superclass be a member of a subclass if it
is not inherited? Further, if it is not a member of the subclass then
how can it be called directly or
indirectly?

I think that what I am getting to is that the language used in the JLS is
somewhat ambiguous. In fact, I think that things
would be much more clear if the term "inheritance" was used
only in reference to classes and not members. The notion of
inheritanting members really does not exist in either C++ or Java rather
members have access restrictions.

In my thinking all members are always exist in their child classes, however
their visability may be restricted.

Maybe - I'm getting "too" hung up on the semantics - but it is a very important
concept to nail down correctly.

Re: Inheritance of Class with Private Memebers?

> Paul 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"
>
> I agree with what you are stating but it appears to be inconsistent with
> the JLS language.
>
> How can a private member of a superclass be a member of a subclass if it
> is not inherited? Further, if it is not a member of the subclass then
> how can it be called directly or indirectly?

Private members of a class are not members of any subclass. So they cannot
be used in code anywhere outside of their class, not even in subclasses.
But they CAN be used in code inside their class, and it's perfectly possible
for that other code to be public, so it can be used from outside the class.
That is how a private member can be called indirectly. This is an extremely
common usage in Java. Example:

in which the private member "age" can be used and changed from outside the
class. The point is that only public members are used to do this.
>
> I think that what I am getting to is that the language used in the JLS is
> somewhat ambiguous. In fact, I think that things
> would be much more clear if the term "inheritance" was used
> only in reference to classes and not members. The notion of
> inheritanting members really does not exist in either C++ or Java rather
> members have access restrictions.

"Inherited" has meaning beyond just membership, it's tied up with object
polymorphism.
>
> In my thinking all members are always exist in their child classes,
however
> their visability may be restricted.

I guess it's reasonable to think of private variables being members of a
subclass that are completely invisible. But there is a significant problem
with this:

Case 2:
Superclass has public method void erase(); subclass has public method
erase().

In case 2, the subclass method overrides the superclass method. But in case
1, it does not override it, it simply hides it. When object polymorphism
comes into play, these two cases behave differently.
>
> Maybe - I'm getting "too" hung up on the semantics - but it is a very
important
> concept to nail down correctly.
>
> David