This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Class access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class access modifiers" Watch "Class access modifiers" New topic
Author

Class access modifiers

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Classes can have only public or default access..

What would happen if private and protected were also allowed for classes?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Let's consider what would happen.

If he class is private, that means that nothing outside the class can see it. Oops. The class has just been rendered completely useless, being completely isolated from the rest of the code since no other code can see it.

If the class is protected, that means that only classes in the same package (default access) and sub classes can see it. But for a class to be able to sub class it, they must see it first. And they can't until they are sub classes. It's like saying "you can enter my house, but only if you're already in it". As such, the protected adds nothing that default doesn't already offer.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Thanks Rob for such a wonderful explanation..
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Rob Prime wrote:Let's consider what would happen.

If he class is private, that means that nothing outside the class can see it. Oops. The class has just been rendered completely useless, being completely isolated from the rest of the code since no other code can see it.

If the class is protected, that means that only classes in the same package (default access) and sub classes can see it. But for a class to be able to sub class it, they must see it first. And they can't until they are sub classes. It's like saying "you can enter my house, but only if you're already in it". As such, the protected adds nothing that default doesn't already offer.
If you want to allow classes in the same package to be able to create instances of, for example, class A and also subclass class A but only allow classes outside of the package to subclass class A (i.e not create instances of class A), wouldn't that be an argument to allow protected classes?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Rob Prime wrote:you can enter my house, but only if you're already in it

You can exit my Klein Bottle, but only if you're already outside it. Inside. Outside? Um...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Tom Reilly wrote:
Rob Prime wrote:Let's consider what would happen.

If he class is private, that means that nothing outside the class can see it. Oops. The class has just been rendered completely useless, being completely isolated from the rest of the code since no other code can see it.

If the class is protected, that means that only classes in the same package (default access) and sub classes can see it. But for a class to be able to sub class it, they must see it first. And they can't until they are sub classes. It's like saying "you can enter my house, but only if you're already in it". As such, the protected adds nothing that default doesn't already offer.
If you want to allow classes in the same package to be able to create instances of, for example, class A and also subclass class A but only allow classes outside of the package to subclass class A (i.e not create instances of class A), wouldn't that be an argument to allow protected classes?

Perhaps it would. Maybe, with enough support, that may be added to the JLS in a future version. After all, it's allowing something that's not allowed now, so it wouldn't break any code.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Perhaps it would. Maybe, with enough support, that may be added to the JLS in a future version. After all, it's allowing something that's not allowed now, so it wouldn't break any code.

I don't know how we got along without this feature all this time. Just think of all its uses! :-)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

I just thought of a reason why this isn't needed - it's already possible. Just make sure that the constructors aren't public. If a constructor is protected then it can only be accessed from the same package and subclasses - but only to call it in their own constructor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class access modifiers