• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classes

 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
This seems to be a very simple and straight forward,but i want a logic for the fact that why are protected classes not allowed while default classes are allowed as top level classes,even though protected is less restrictive as compared to default?

Thanks...
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read Class access modifiers.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
default: If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. This access-level is convenient if you are creating packages.

protected: Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

This may help you.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original question was about why you can’t have a protected top-level class.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes in different packages cannot access classes that are protected. so it is meaningless to have the top level class protected. we already have default access to provide access for classes within same package
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic