Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modifier protected does not hide inherited members to (static) code of superclass package

 
Alexander Exner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&Bs Book tells me on Page 36:
Once a Subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclass of the subclass.

I did ask myself what happens if a class in package B extends a class from package A and another (e.g. static) method within package A tries to use some protected stuff from B:







I would have assumed that doing ((AClass)b).m(); is ok, because BClass extends AClass and AClass has method m(). m() from BClass should be invisible for "any code outside the subclass", therefore output should be perhaps "A". In fact output is "B\nSuper:\nA".

Please can anybody explain java to me?

KR Alex
 
Garg Sharad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Output of below statement is correct because
((AClass)b).m(); // Does compile and return B\nSuper:\nA

There are two things happening

one is compile time polymorphism.
It's happening because you are doing BClass object type cast with AClass object so this thing checked at compile time.

second run time polymorphism.
At run time even you are using sub class object with super class reference it'll call sub class object method.



You are getting compile time error at

b.m(); // Does not compile

line because method m access modifier is protected and you can not use out of protected member out side the package without using inheritence.



 
Alexander Exner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your clarification, looking at compile time and run time separate explains behavior.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have posted my query in new post as previous post was resolved
i have changed the code a bit.

AClass.java


BClass.java


Test.java


OUTPUT:
A
B
Super:
A
A

K&Bs Book tells me on Page 36:
Once a Subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclass of the subclass.


here,m is protected member of AClass that is inherited by BClass.so,by above statement m in BClass would be Invisible outside BClass.
but ,it is accessible in Test.java

SEE LINE 8,11

how is this possible ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garg Sharad wrote:

one is compile time polymorphism.
It's happening because you are doing BClass object type cast with AClass object so this thing checked at compile time.


Compile time polymorphism is for overloaded methods.

Check this JLS regarding the protected modifier.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic