• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Protected access modifier at class level

 
Ranch Hand
Posts: 44
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why "protected" can't be used as access modifier at class level whereas "default" can be used which is more restrictive than "protected" ? For example:-




 
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Top level classes cannot be protected because protected members are accessible only in same package in its and sub classes in other package. At top level these are meaningless.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain what it is supposed to mean if a top-level class would be 'protected'?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajat,

I broke up that enormous line in your code. Please don't put very long lines inside code tags; it screws up the windowing.
In fact, I suggest you give the UseCodeTags page another read.

Thanks

Winston
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we make a class as "protected", I know it's not a good programming practice to have a class as "protected" but I don't understand why it is not allowed. Since "protected" lies between "public" and "default" access specifier. What's made Sun to remove "protected" from class level access specifier. Please give me some relevant information. Thanks in Advance.
Please don't mention answers like what's the use of having "protected" as a class level access specifier.
 
Bartender
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajat Jindal wrote:Please don't mention answers like what's the use of having "protected" as a class level access specifier.



Why not? Most answers to the question "why isn't this keyword allowed here?" are "because it wouldn't make any sense". The best way to approach these questions is to try and work out what it would mean if it was allowed, and often that provides the answer.

So, if protected was allowed here, what would you expect it to mean? Here's what I'd expect. I'd expect it to be the same as default, except the class would be visible to subclasses. But how do you subclass something that isn't visible already?
 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Can you explain what it is supposed to mean if a top-level class would be 'protected'?



Suppose the top level class is protected in that case we have the advantage of default(friendly) behavior and plus access to sub classes in another package but in that case there will be a problem that for the subclass of another package that top level class will we like private . Basically outer sub classes cannot access the PROTECTED top level class therefore it become meaningless.
 
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Top level class can either be declared as public or package. Thus protected top level class is not VALID.
 
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:I'd expect it to be the same as default, except the class would be visible to subclasses. But how do you subclass something that isn't visible already?



Its a perfect answer to "Why can't we have the 'protected' modifier for a top-level class". Assume it's allowed to use protected modifier for a class. Then what will happen, it will be visible to all the classes in the same package which is the same behavior what a default (package-level) access class will possess. Additionally this 'protected' class should be visible to all the subclasses outside package also. But unfortunately you would not be able to create any subclass of this class outside the package because this class itself will not be visible outside the package. Hence without the subclass specific behavior, this 'protected' class will be exactly same as a package-level or default access class. So, there is absolutely no need of 'protected' modifier for classes and hence, not permissible as well.

Hope, it helps although I just tried to elaborate Matthew's point.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic