wood burning stoves 2.0*
The moose likes Java in General and the fly likes Access specifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Access specifiers" Watch "Access specifiers" New topic
Author

Access specifiers

deepikakasi kasi
Greenhorn

Joined: Oct 08, 2006
Posts: 7
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The same applies to nested classes.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
I think Ilja wanted to remind us, that inner classes can also be private or protected.


all events occur in real time
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access specifiers