Q on Classes & SubClasses with protected members

P

Praveen

Hi..All,

Can someone please explain me this

"Access to protected members of the superclass would also be permitted
via any references of subclasses. The above restriction helps to ensure
that subclasses in package different from their superclass can only
access protected members of the superclass in their part of the
inheritance hierarchy".

Why is this restriction for class in a different package extending a
class from a different package?

This is from Khalid A Mughal's book.



Regards,

P
 
W

Woebegone

Praveen said:
Hi..All,

Can someone please explain me this

"Access to protected members of the superclass would also be permitted
via any references of subclasses. The above restriction helps to ensure
that subclasses in package different from their superclass can only
access protected members of the superclass in their part of the
inheritance hierarchy".

Why is this restriction for class in a different package extending a
class from a different package?

This is from Khalid A Mughal's book.



Regards,

P

If I understand correctly, the paragraph refers to "package private"
members -- members, declared with no access specifier, that appear public to
other members of the declaring class's package, but private to members from
external packages. This allows for some degree of coupling due to
implementation, but prevents implementation details from "leaking out" via
subclassing. It is common when implementing a component for example, that it
comprises multiple classes that must collaborate while presenting a unified
interface. The use of package private members allows the collaborating
classes to communicate with each other as necessary, but prevents clients
from writing code that depends on those members. Clients should program to
the component's interface only.

The stipulation about subclasses prevents using extension to circumvent the
encapsulation: say a component C is implemented as a collaboration of
classes A and B. In some cases, I could get access to implementation details
by writing my own subclass of A and installing it in C. For a concrete
example, imagine C being a logon dialog. In principle, I could subclass one
or more of the classes used by the dialog, creating a new version that
transmits to me the user's id and password.

That example might be a bit contrived, but there are many examples in Swing
of using package private access among classes that make up the components.

HTH!
Sean.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top