• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access modifier problem  RSS feed

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why cant we place "private" in the top class??
Any one help me please
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since "private" means "can't be accessed from any other class", of what use would a private class be?

Top-level classes can be public (meaning visible outside their package) or not (meaning not visible outside their package.) These are really the only meaningful options.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

Top-level classes can be public (meaning visible outside their package) or not (meaning not visible outside their package.) These are really the only meaningful options.


Why is "protected" not a meaningful option?
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bianchi Francesco:
Why is "protected" not a meaningful option?


What would "protected" mean for a class?
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:


What would "protected" mean for a class?


Exactly what "protected" means for a method or a variable. A class that would be visible by others in the same package and by subclassing ones.

Probably such a case is very rare and symptom of a probable bad design choice..but I'm not so sure about this...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a viable option, yes. But you can't introduce these protected classes without introducing private inheritance -- i.e., if only a class knew about its superclass, then the inheritance relationship would be secret outside the package. Java doesn't have private inheritance; it's a messy part of C++ that Java's designers deliberately left out.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bianchi Francesco,

But doesn't the default scope (i.e. friendly) provide the same visibility (i.e. visible in same package and subclasses)?

Edited Later: I realized later that "protected" offers you visibility outside package IF the subclass exists outside package, something that the default scope wouldn't allow.
[ July 11, 2007: Message edited by: Shyam Prasad Murarka ]
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
That's a viable option, yes. But you can't introduce these protected classes without introducing private inheritance -- i.e., if only a class knew about its superclass, then the inheritance relationship would be secret outside the package. Java doesn't have private inheritance; it's a messy part of C++ that Java's designers deliberately left out.


I have always asked myself why they decided to omit to give this chance to developers. Thanks for giving me the answer
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!