Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Java Access Specifiers (http://www.go4expert.com/articles/java-access-specifiers-t28019/)

Debasmita 14Mar2012 16:46

Java Access Specifiers
One of the prime features of object oriented programming is data encapsulation.This feature enables data to have restricted access.There are currently four different access specifiers in JAVA. Namely,
  1. Public
  2. Protected
  3. Default
  4. Private

Public:- All the data members and member functions which are declared public can be accessed from anywhere outside or inside the class which means it can be accessed by any object or through any member functions.It is also called a class level access modifier as class is usually made public in JAVA so that it can be accessed from anywhere.

public class A{  //public class
    public a; //public variable declaration
    public call( ); //public method declaration

Private:- All the data members and member functions which are declared as private can be accessed only from that specific class where it belongs. It cannot be accessed from anywhere else outside that class whether it is an object,function or a class. Thus private methods and fields cannot be inherited by any subclass and thus cannot be accessed by any sub-class.

Protected:- Any data member,member functions which are declared as protected can be accessed from within that class and any subclass of that specific class to which they belong whether it is in the same package or different package. The access level remains unchanged while inheritance from one class to the other in JAVA.

Default( No specifier):- Any data member,member functions which has no access specifier mentioned to it , it can be accessed from within that package where the class,method or field belongs but not from anywhere outside that package. Sometimes there are no access levels mentioned to a class and this is when the class can be accessed from anywhere within the same package only.

Following is the tabular representation for Java access specifiers


Ajay12 8May2012 17:28

Re: Java Access Specifiers
Hai Debasmtia !!!!!!!!!!!!!!!!
This is Ajay.
Very precisely & shortly you discribed the Java Access Specifiers.
Very good.

Can you help me ?
I have added in yesterday. I am new.
I am computer degree student of 4th Sem. I also have some nice articles.
But I don't know how to post these articles or here it is said here as a 'Thread'.
Please help.
I shall be thankfull to you.

mfred90 22May2012 14:16

Re: Java Access Specifiers
Java Access specifiers are placed in front of each definition for each member in your class, whether its a field or a method eaech acess specifier controls the access for only that particular definition. Java supports four types types of access specifiers : private, protected, public, and if unspecified, package access.

icool 2Jan2013 15:56

Re: Java Access Specifiers
Very informative post.
The table with access modifiers is very useful.

All times are GMT +5.5. The time now is 06:13.