Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Top level class Vs inner classes access specifiers

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why top level class cannot be private or protected and inner classes can be private or protected?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that is the way it is. What would you do with a top level private class ? Can you even use one ?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inner classes have most strange concepts that are not applicable to regular class sometimes

like using abstract and static simultaneously (legal)

also other modifiers(private and protected) that applied only to inner class.(legal)
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun Srivastava wrote:
like using abstract and static simultaneously (legal)

Some inner class needs this behavior, but for outer classes, it doesn't make sense!
 
Gari Jain
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna

Innerclasses are defined within another class and they are like members of the enclosing class. So just like the members of a class can be private or protected too, innerclasses can also be private or protected, just like methods of a class(because they are members of the class) can use the private members of the class, that is why innerclasses can also access the private members of the class.

One reason for the behaviour shown by innerclasses is because they are members of the enclosing class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic