• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Protected Access Modifier

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

Can anyone please tell me why cant we have protected access modifier for a class?



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

You cannot make a top-level class protected, because it wouldn't make any sense. The protected access modifier, when used on methods or fields, means: visible only to classes inside the same package and to subclasses.

You can make inner classes protected:
 
Krutika Ravi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You cannot make a top-level class protected, because it wouldn't make any sense. The protected access modifier, when used on methods or fields, means: visible only to classes inside the same package and to subclasses.



Hey Jesper,
Thanks for replying. I'm still an amateur in Java, so excuse me if my question sounds silly.
Why then can we use default which is even less restrictive than protected.
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why then can we use default which is even less restrictive than protected.


default access is more restrictive than protected.
protected = package access + subclass (through inheritance)
default = package access ONLY
public -> protected -> default -> private
 
Krutika Ravi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry My question was
Why then can we use default which is even more restrictive than protected.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you think it should mean if you could make a top-level class protected?
 
Krutika Ravi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why one can do with a protected access specifier for a top-level class?
Ans.Lets suppose I have a class containing only protected members.Usually one encloses its protected members inside a public class,say in package1. Now if i import this package in another package say package2, the only way i can use my protected members(of the class in package1) is through inheritance and not through reference, though we can create an object of that class because it was public in nature but certainly we can never ever access its members through a dot operator. Hence theres no point having a public class for a protected members after all. Why not make use of protected access specifier for class to indicate that this class can only b inherited in another package. Besides Protected is all about inheritance in another package and yet we cannot have a protected class.

Many people have told me protected class doesn't make any sense(which i still can't figure out why).But having a public class for protected members doesn't either.
My questioning may seem unreasonable,hoping you guys bare with it.
We all here to learn
 
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic actually started out as very interesting, IMHO... I was thinking it could go to a loophole in the specification. Maybe a discussion on how the absence of a modifier (default modifier) isn't really a modifier. Or maybe a discussion on public and default is an artifact of how the java compiler compiles classes. etc.

This direction, on how protected could be used... isn't as interesting. It sounds like a feature that will never be used to me.

[BTW, this last part is completely opinion. I am sure that there are others who believe that package access levels is important, and really like this idea.]

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic