Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Public class

 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt reg access specifier.

If a class is declared as public will it hold the same for its constructors and its methods implicitly?

thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they will all have default (package) access modifiers. Exception for a defaulted implicit no argument constructor - that will have an implicit public constructor if the class is public.
 
Ramesh Mangam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jenny,

Class declaration has nothing to do with method declaration. You have to expicitly give access modifiers to constructors or methods. However, there is one exception. If we do not write any constructors ourselves, in that case the compiler provides us with default constructor and the access modifier for that constructor will be same as the its class's access modifier. Also, a class can have only public or default access levels.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Also, a class can have only public or default access levels."

For top-level classes, yes. But not nested classes.
 
Ramesh Mangam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I should have been more specific. Thanks Barry.
 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks,that was a clear explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic