• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Members with default access

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can someone please help and tell me the reason why this is allowed?

A default access member(package level access) of SuperClass is inherited by the sub class, if both are in the same package.

 
MyExamCloud Support
Posts: 264
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himanshu Mall,

If the variable is declared without any explicit modifier, it is referred as package-private. The package-private variable is visible only within its own package.

Here, both Parent(super class) and Child (sub class) are in same package ( certification ), hence the variable x is inherited by package-private rule.

Thanks
 
Himanshu Mall
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ganesan,

AFAIK Inherited Members in a subclass ( Protected access modifier) and whether you can access a member (using reference ) are two different things.

My question is that why a sub class inherits a default access level member from its super class, if both are in the same package ( Since package or default access level has nothing to do with Parent Child relationship, Why this subtle behavior).

Note: Access level is fine, since both are in same package, sub class or any other class in the same package can access the default access member (of parent or any other class ) through its reference.

Please correct me if i am wrong.

Thank you
Himanshu Mall
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himanshu Mall wrote:
My question is that why a sub class inherits a default access level member from its super class, if both are in the same package ( Why this subtle behavior).



Why shouldn't it? That's how the rules of Inheritance work right?
Update:
And again if the classes are not in the same package then the default members are not accessible via the inheritance.
 
Himanshu Mall
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okk, thanks...

I missed it because its never mentioned explicitly in the books that i read (including Core Java, Sun Edition). Glad that now i know this.
So now we can say that:

" A sub class inherits the protected members of its Super Class and members with default level access (package level), if and only if both Sub Class and Super Class are in the same package".

Let me know if i missed something.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, just to clarify again: For members with default access both Sub and Super class have to be in same package.

One tip: You can quickly write a simple (very simple) code and try out these. It will really help you if you are preparing for SCJP.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic