IntelliJ Java IDE
The moose likes Beginning Java and the fly likes Classes with default package access but containing public members Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Classes with default package access but containing public members" Watch "Classes with default package access but containing public members" New topic
Author

Classes with default package access but containing public members

Rik Roos
Greenhorn

Joined: Feb 06, 2012
Posts: 13
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

Joined: Jan 03, 2004
Posts: 3133
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

Joined: Feb 06, 2012
Posts: 13
@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

Joined: Jan 03, 2004
Posts: 3133
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

Joined: Feb 06, 2012
Posts: 13
Oke Jeff, thanks for explaining.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 2771

En welkom bij CodeRanch, Rik ;)
Viktor Kubinec
Greenhorn

Joined: Jan 28, 2012
Posts: 28
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

Joined: Feb 06, 2012
Posts: 13
@Stephan
Dank je wel !

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

greetz
Rik
 
 
subject: Classes with default package access but containing public members
 
Threads others viewed
default members
doubt in access modifier
confuse about protected and static
Is there a solution for it?
Overloading and overriding the finalize method
IntelliJ Java IDE