Q on Classes & SubClasses with protected members

Discussion in 'Java' started by Praveen, Nov 20, 2003.

  1. Praveen

    Praveen Guest

    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
     
    Praveen, Nov 20, 2003
    #1
    1. Advertising

  2. Praveen

    Woebegone Guest

    "Praveen" <> wrote in message
    news:W0Yub.9088$...
    > 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.
     
    Woebegone, Nov 21, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Dyl
    Replies:
    2
    Views:
    483
    Mr Dyl
    Dec 1, 2005
  2. Daz
    Replies:
    5
    Views:
    583
  3. Sideswipe
    Replies:
    6
    Views:
    378
    Daniel Pitts
    Sep 20, 2007
  4. aidy
    Replies:
    11
    Views:
    213
    Robert Dober
    Jul 22, 2008
  5. Kevin Prichard
    Replies:
    11
    Views:
    410
    Richard Cornford
    Nov 29, 2005
Loading...

Share This Page