Core Java Interview Questions and Answers
<< Previous Question Next Question >>
 Question: 2053 Page Views: 

What are the variuos Access Specifiers available in Java?



Posted By: Avi Date: 9 November 2009 06:29:07 AM
 Answer:

Java offers following four access specifiers:

1) public:
public classes, methods, and fields can be accessed from everywhere.

2) protected:
protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.

3) default(no specifier):
If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.

4) private:
private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses.


Posted By: eTechPlanet


Date: 9 November 2009 06:29:07 AM

Java has four different access specifiers: public private package and protected !!!

private : The most restrictive of the access specifiers is private. If a variable [or a method] is declared private then it can only be accessed by methods in that class.

public : The least restrictive of the access specifiers is public. If a method has public in front of it then any object of that class can be told to perform that action.

package : Every class that is in the same package has access to the fields and methods in your class.

protected : If you have a variable or method in your class that you don't want clients of your class directly accessing declare it protected.


Posted By: Johndecruse


Date: 17 February 2010 12:23:35 AM
Post a better Answer if you have
 
(Will show your Gravatar icon)  
  Country flag

Loading
Enter the text as shown in the image WxQDLS
Related Questions
Core Java : Can we create an abstract class with no abstract methods in it?

Can we create an abstract class with no abstract methods in it?

Yes, we can create an abstract class without abstract methods.
Category: Core Java Date: 11/9/2009 6:20:54 AM
Core Java : What is the difference between "while" and "do while" statements in Java?

What is the difference between "while" and "do while" statements in Java?

"while" statement checks the condition at the beginning of a loop to see whether the next ....
Category: Core Java Date: 11/9/2009 6:16:44 AM
Core Java : Expain each keyword of public static void main(String args[])?

Expain each keyword of public static void main(String args[])?

public: main() is the first method called by java environment when a program is executed so it has t....
Category: Core Java Date: 11/9/2009 6:12:58 AM
Core Java : What gives java it's "write once and run anywhere" nature?

What gives java it's "write once and run anywhere" nature?

All Java programs are compiled into class files that contain bytecodes. These byte codes can be run ....
Category: Core Java Date: 11/9/2009 6:09:51 AM
Core Java : What method must be implemented by all threads?

What method must be implemented by all threads?

All threads must implement the run() method, whether they are a inherite from Thread class or implem....
Category: Core Java Date: 10/30/2009 5:10:31 AM