Re: Why does compiler only look at public methods of superclasses
of...?
failure_to@yahoo.co.uk wrote:
a) Why does compiler only look at public methods in superclasses of
A?
Well, it doesn't really, but basically that's "by design." In other
words programmer need some way to encapsulate code, and the
public/private modifiers were chosen by Sun as the way to implement
encapsulation.
c) If compiler does look for both default and public methods in A's
superclasses, then why doesn't it also look for methods with protected
access modifier?
Your book is defective. Get a better one. In particular:
Default access methods are "package private." They can be accessed like
public methods from other classes in the same package. From outside a
package, the are effectively private.
Protected methods can be access only by classes which are subclasses of
the class. Everything else is sees them as effectively private.
Why point out that if method is private then compiler knows what
method to call? I'm assuming that book is only talking about the case
where private method x() is defined in class A and not in one of A's
superclasses?!
Yes it has to do with inheritance. However, since private methods are
only accessible by the class itself (or an inner class), it really only
has to do with name resolution inside of the class or a subclass. You
might want to check that out and make sure you understand the different
types of inheritance too, as well as accessibility modifiers.
* This may be a silly question, but why can't you override a protected
method? Surely there is some very good reason for java developers
deciding not to allow this?!
See protected methods above.