Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class access modifiers

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes can have only public or default access..

What would happen if private and protected were also allowed for classes?
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for such a wonderful explanation..
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic