aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Modifier protected does not hide inherited members to (static) code of superclass package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Modifier protected does not hide inherited members to (static) code of superclass package" Watch "Modifier protected does not hide inherited members to (static) code of superclass package" New topic
Author

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

Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
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

Joined: Jun 20, 2009
Posts: 15
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.




Cheers,

Sharad Garg| SCJP6 95%
Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
Thanks for your clarification, looking at compile time and run time separate explains behavior.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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 ?


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Modifier protected does not hide inherited members to (static) code of superclass package