• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

top level class access modifier woes

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why it 'doesn't make sense' to modify a top level class with the protected access modifier.

why does it make sense for classes to be accessible to other classes in the same package only (default) but not to other classes in the same package and subclasses in other packages (protected)?

can somebody please explain?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I also face this dilemma whenever I read this statement. Can some one please explain?

Murali...
SCJP5
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom House:
I don't understand why it 'doesn't make sense' to modify a top level class with the protected access modifier.

why does it make sense for classes to be accessible to other classes in the same package only (default) but not to other classes in the same package and subclasses in other packages (protected)?

can somebody please explain?



Be careful with that statement.
but not to other classes in the same package and subclasses in other packages (protected)?

Ok suppose Java allows top level class to be protected. What will you use it for. It becomes public access modifier only.

If suppose you make a class as protected then only the classes that subclass this class will be able to use it which in turn means that this class has to be public so that any class in any other package can see it and hence subclass it.

Think deeply man, its a thin difference!!!
 
Tom House
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandeep Bhandari:
[QB]
Ok suppose Java allows top level class to be protected. What will you use it for. It becomes public access modifier only.

If suppose you make a class as protected then only the classes that subclass this class will be able to use it which in turn means that this class has to be public so that any class in any other package can see it and hence subclass it.
QB]



whoa whoa whoa - wait a minute.... if a top level class was protected and subclassed by another protected (or public or default) class in another package, then why couldn't the classes in that other package access the subclass? Isn't that a design decision that would have to be made if protected top level classes were allowed?

on a related topic - how much wood could a woodchuck .......?
 
Tom House
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom House:


whoa whoa whoa - wait a minute.... if a top level class was protected and subclassed by another protected (or public or default) class in another package, then why couldn't the classes in that other package access the subclass? Isn't that a design decision that would have to be made if protected top level classes were allowed?



ok - wait, a light might be donning here. if a protected class was subclassed into a new package then the subclass would be able to access it but nothing else would be able to access the subclass because the original class maintains it's protected status from it's original package - is that right?

What about a protected class with only private members? that could work right?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic