wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Modifier protected does not hide inherited members to (static) code of superclass package Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Modifier protected does not hide inherited members to (static) code of superclass package Query" Watch "Modifier protected does not hide inherited members to (static) code of superclass package Query" New topic
Author

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

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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%
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
public class BClass extends AClass {
private int m=12;
[public [/b]void m1()


You have made it public. How could it be private?

Cheers
Aneesh


Cheers
Aneesh
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i am talking about m method and not m1
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
Mohit,

Your Test.java & AClass.java are using the same package (package A)

If you expect this not to work, then you should override the m method in BClass.java

Hope this clears.

Cheers
Aneesh
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Make m() method as private in the class AClass, and see what happens!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Alexander Exner
Greenhorn

Joined: Jul 30, 2010
Posts: 10
AFAIK this happens:
1) Compiler indeed does not see inherited protected method m() in package B (BClass).
2) Therefore compiler looks at superclasses and finds protected method m() as defined in package A (AClass). Compiler is happy with that because its the same package as the "caller".
3) Runtime behavior was in my original example (http://www.coderanch.com/t/504882/java-programmer-SCJP/certification/Modifier-protected-does-not-hide) different, but in this example the JVM has only one choice as well: m() from AClass.

Maybe K&B should go a little bit more in detail on page 36...

KR Alex
 
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 Query
 
Similar Threads
Possible error in K&B book (resolved - no error)
Crazy protected modifier: K&B book lies?
Q on overriding
protected doubt
Modifier protected does not hide inherited members to (static) code of superclass package