Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why can't be protected class ?

 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java class can be public and default, whereas it cant be private because it'll be useless but why it'cant be protected.I don't believe in rules although i believe in reason, please satisfied me .
 
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Nandwana wrote:Java class can be public and default, whereas it cant be private because it'll be useless but why it'cant be protected.I don't believe in rules although i believe in reason, please satisfied me .



Inner classes can be protected. Only classes in the same package and subclasses of the outer class in different packages would then be able to access the protected inner class.
 
Saloon Keeper
Posts: 13868
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a member is protected, it means that subclasses of its enclosing class can access it. But a top level class doesn't have an enclosing class, so protected makes no logical sense.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:If a member is protected, it means that subclasses of its enclosing class can access it. But a top level class doesn't have an enclosing class, so protected makes no logical sense.



thanks but can you clear it?
Suppose in a package there are two classes ,first class has protected access modifier and second one has default now i try to extend second class ok ? now what is theoretically illegal in it ? because both is in same package and because of protected access modifiers of superclass it should allow to access its member to subclass ,there shouldn't be any problem
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that would just be the same as default access.

Think about the difference between protected and default access. That's what's pointless for a top-level class. And since it's pointless, it might as well be made illegal.

Most of the questions along the line of "why isn't that modifier allowed here" can be answered by asking "what would it mean if it was?". Usually it just wouldn't make a lot of sense.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:But that would just be the same as default access.

Think about the difference between protected and default access. That's what's pointless for a top-level class. And since it's pointless, it might as well be made illegal.

Most of the questions along the line of "why isn't that modifier allowed here" can be answered by asking "what would it mean if it was?". Usually it just wouldn't make a lot of sense.



OMG how could anyone have a such big brain. excellent answer
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic