Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access specifiers

 
deepikakasi kasi
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an Interviewer is asking the appropriate difference between protected,private,Default & public access specifiers.

Is it enough to answer like this..

public - Accessible to all packages
private--Accessible only within that class
protected--subclass can access superclass evenif it is in other packages.
Default -- cann't access in different packages..can be accessed if it is only in same package.

can U please provide some more information regarding this.....

thanks & regards,
Deepika.K.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More specific:

Classes:
public - accessible by all classes in all packages.
default - accessible by classes in the same package only.

Methods and instance variables:
public - accessible by all classes in all packages.
default - accessible by classes in the same package only.
protected - accessible by classes in the same package only and accessible by subclasses in all packages.
private - accessible by the parent class only.

Local variables are accessible by the parent method only.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bauke Scholtz:
More specific:

Classes:
public - accessible by all classes in all packages.
default - accessible by classes in the same package only.


You forgot about nested classes...
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same applies to nested classes.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ilja wanted to remind us, that inner classes can also be private or protected.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, you're right. Further on, don't forget about interfaces, nested interfaces, enums and nested enums.
[ October 12, 2006: Message edited by: Bauke Scholtz ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic