• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classes with default package access but containing public members  RSS feed

 
Rik Roos
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I was thinking about the possibility of marking the class-members with the public-access modifier while the embedding class itself has default (package) access.
That makes no sense in my humble opinion. However, the javac-compiler eats it and exits silently.

Is there a reason why one can mark a member as public but the class is having default access?

Greetz
Rik
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not that uncommon. Even private classes have public members. The main use is in implementing an interface. You want to control access to the implementing class, but if it's gonna implement an interface, those methods must be public.
 
Rik Roos
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeff

Oke, implementing some interface, that is clear to me.

But writing a default-access class without implementing any interface and still using public members seems a bit silly to me. Do you agree on this?
Any extending subclass of this default-access class can choose to make the overridden methods public, so no reason to make the super-members also public.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rik Roos wrote:@Jeff

Oke, implementing some interface, that is clear to me.

But writing a default-access class without implementing any interface and still using public members seems a bit silly to me. Do you agree on this?


Off the top of my head, if it's not implementing an interface or extending a class that has public members, no, I can't think of any good reason for doing so.

I don't know. Maybe there might be a situation where you'd want the members to be available, but not the class. I can't think of a specific real world use case like that, but who knows, somebody may have one.

Either way, though, since we have the interface case, that right there provides an answer to your question as to why it's allowed by the language.

Any extending subclass of this default-access class can choose to make the overridden methods public, so no reason to make the super-members also public.


Yes, a subclass can always make its inherited members more accessible. It just can't make them less so.
 
Rik Roos
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oke Jeff, thanks for explaining.
 
Stephan van Hulst
Saloon Keeper
Posts: 7961
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
En welkom bij CodeRanch, Rik ;)
 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rik Roos wrote:@Jeff

Oke, implementing some interface, that is clear to me.

But writing a default-access class without implementing any interface and still using public members seems a bit silly to me. Do you agree on this?
Any extending subclass of this default-access class can choose to make the overridden methods public, so no reason to make the super-members also public.



I think that you have just answered your question. If you want to extend default class with public class and you want to have these methods to be public, you can mark them public in the default class and then you don't have to override them in the public subclass.
 
Rik Roos
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan
Dank je wel !

@Viktor
Indeed, I think it all depends on what is desired. So it gives only extra potential.

greetz
Rik
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!