• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your clarification, looking at compile time and run time separate explains behavior.
 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic