tags:

views:

161

answers:

4

I just noticed this behavior today - weird, I'm pretty sure in java a you can only access protected methods upstream on the inheritance chain since going the other way violates encapsulation.

Was there a reason for this behavior in the language?

A: 

This is allowable in Java as well. This is probably allowed in Java however, since protected is also considered to be package level scope and not just relegated to access within the inheritance chain.

mlaw
+1  A: 

This link may be helpful for your answer:

http://www.velocityreviews.com/forums/t129550-why-is-a-superclass-allowed-to-access-protected-methods-of-a-subclass.html

I hope it helps you.

lepe
thanks a bunch, lepe
ambertch
you are welcome :)
lepe
+1  A: 

I've found it useful when one method defined in the parent only needs a small part of its functionality to change based on the extension class type. You can call an abstract method from within the parent, and it's functionality changes as needed with the definition of that method in the child classes.

I would also add that sibling classes can also access each other's protected properties and methods, as long as they are declared in the parent class (this can be abstract or not).

keithjgrant
Keith, with sibling classes allowed to access each other's inherited protected properties, do you know if this varies among languages? If in Java 'protected' means package level access, two sibling classes with self defined (not inherited) protected properties are still in the same package, but they can't access eachother?Thanks
ambertch
A: 
protected != private
powtac