• 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

Class access modifiers

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob for such a wonderful explanation..
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic