• 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

Why cant a java class have protected access modifier....???

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a certain idea that when any entity is declared as protected, it is not visible outside the package unless the class is extended. So if the class itself is declared as protected then the class cant be extended outside the package as the class is not at all visible outside the package. This is as good as declaring the class with default modifier. Therefore it doesnt make sense to declare a class protected and hence the limitation.

Is what i think correct or is it a wrong presumption by me...
If it is wrong can someone please give me the right answer...
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by waseem nadaf:
So if the class itself is declared as protected then the class cant be extended outside the package as the class is not at all visible outside the package.



Class cannot be protected , it can only be public or default.

And if the JLS had something like to have a class protected then it would have been very much possible for the classes outside the same package to extend the protected class.
 
waseem nadaf
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rahul,
I understood your point....

But you said "Class cannot be protected , it can only be public or default."

My question is why cant a class be potected...??
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer would be "because the JLS defines it that way".

In which situation would it make sense to define a class as protected? Think about how that modifier determines the visibility of fields and methods, and especially how it is different from default (i.e., package) visibility. How would it apply to a class?
 
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . but classes can also be declared as private.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only with inner class.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And an inner class can be defined "protected", because it actually makes sense in that context. A protected inner class can be accessed by subclasses of the outer class (and also by the package).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic