Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

use of protected access

0 views
Skip to first unread message

logica.com

unread,
Jan 7, 2000, 3:00:00 AM1/7/00
to
Sun's Java tutorial describes a caveat with the use of the 'protected'
access modifier.

It goes like this. There are 2 classes in an inheritance hierarchy (see
below) called Mammal and Bear, and each class is in a different package. I
have a method called sleep() defined in Mammal class, which is naturally
inherited (not overriden) by the Bear class.

If I have a main() method in the Bear class, which instantiates a Mammal
object, I cannot call the sleep() method directly through the Mammal object
reference. Instead, one must instantiate the Bear class, and call the
sleep() method through the Bear object reference. (This is the caveat.) The
compiler will through up an error on this attempt.

My question is, why is this protection needed on the Mammal version of
sleep(), when this same method is invoked but through Bear. Surely the point
about 'protected' is that only subclasses (or classes in the same package)
can access the protected members. In this case Bear *is* a subclass.

The tutorial also clearly highlights that member visibility is all about
class boundaries and not object boundaries. However, it is known explicitly
at compile time that Bear is a subclass of Mammal.

CODE ----->

===== Mammal.java =====

package ZooAnimals;

public class Mammal {

protected void sleep() {
System.out.println("Mammal.sleep()");
}
}

===== Bear.java =====

package Animals;

import ZooAnimals.*;

public class Bear extends Mammal {

public static void main(String[] args) {
Mammal m = new Mammal();
// m.sleep(); // ** COMPILER REJECTS THIS **

Bear b = new Bear();
b.sleep(); // ** OKAY **
}
}
=============
Thanks in advance,

Tony Alicea

unread,
Jan 7, 2000, 3:00:00 AM1/7/00
to
@logica.com wrote:
>
> Sun's Java tutorial describes a caveat with the use of the 'protected'
> access modifier.
>

[ . . . ]

Ah yes, the *protected* access modifier... It's not as straightforward
as some people may think. I refer you to page 63 of the book THE JAVA
PROGRAMMING LANGUAGE, 2nd ed. by James Gosling and Ken Arnold. The
section number is 3.2 and the its title is "What Protected Really
Means".

---
Tony Alicea
Software Engineer
Orlando, FL
(USA)

Arved Sandstrom

unread,
Jan 8, 2000, 3:00:00 AM1/8/00
to
In article <854voe$c...@romeo.logica.co.uk>, <@logica.com> wrote:

> My question is, why is this protection needed on the Mammal version of
> sleep(), when this same method is invoked but through Bear. Surely the point
> about 'protected' is that only subclasses (or classes in the same package)
> can access the protected members. In this case Bear *is* a subclass.
>

Your statement is incomplete. The _point_ about "protected", in full, is
that subclasses that inherit the protected member can only access it
THROUGH an instance of themselves. If you leave out that last part then
it's not a proper statement of the meaning of protected.

What your real argument seems to be is, why is "protected" the way it is?
Well, we'll all get old quick trying to deliberate stuff like that. :-)

Arved Sandstrom

0 new messages