Re: why is a superclass allowed to access protected methods of a subclass?
From: Woebegone (woebegone_at_cogeco.ca)
Date: 12/14/03
- Previous message: Andrew Thompson: "Re: URGENT Help With Scientific Calculator!"
- In reply to: Pat Ryan: "Re: why is a superclass allowed to access protected methods of a subclass?"
- Next in thread: Chris Uppal: "Re: why is a superclass allowed to access protected methods of a subclass?"
- Reply: Chris Uppal: "Re: why is a superclass allowed to access protected methods of a subclass?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 14 Dec 2003 01:25:35 -0500
> 4) a number of people have explained/justified the behaviour in terms of
> polymorphim. I disagree - in my opinion, - encapsulation is a compile
time
> issue - its about the compiler enforcing rule of visibility - so it's
> determined by the object's static type and its package. Polymorphism is
> purely a run-time thing - not the realm of the compiler at all. It seems
to
> me that fact that the compiler allows access to a protected method on an
> object outside the pacakge is an odd exception to the general rule, and
the
> fact that the subtype is a related type is not a strong enough reason to
> justify it.
>
>
Hi Pat,
I think you've raised a very interesting question, and I have to admit I was
surprised to learn of the package visibility of protected methods. In C++
terms, it's like package colleagues are friends -- I though encapsulation
would dominate.
But, consider the case where a superclass declares an abstract method, then
calls it as in a factory method as mentioned earlier. Any concrete subclass
must provide an implementation of the method, so the superclass knows a
priori it's there at runtime (this is a runtime rule inferable at compile
time). The code that executes will be that of the runtime object's class:
this is why I said earlier that the superclass implicitly can access the
sublass's implementation.
Then, we know in Java that it's impossible to call up the hierarchy to
arbitrary depth: it's "this," or "super." As an earlier poster mentioned, no
class can infer at which level a member is implemented.
So, I think that the compiler has to behave the way it does to accommodate
polymorphism, while doing what it can to enforce encapsulation. Polymorphism
is a runtime thing, but it requires compile time support.
Protected access supports polymorphic hierarchies; package access is, as I
see it anyway, a pragmatic violation of encapsulation.
Regards,
Sean.
--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.548 / Virus Database: 341 - Release Date: 05/12/03
- Previous message: Andrew Thompson: "Re: URGENT Help With Scientific Calculator!"
- In reply to: Pat Ryan: "Re: why is a superclass allowed to access protected methods of a subclass?"
- Next in thread: Chris Uppal: "Re: why is a superclass allowed to access protected methods of a subclass?"
- Reply: Chris Uppal: "Re: why is a superclass allowed to access protected methods of a subclass?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|