Java Access Specifiers

Discussion in 'Java' started by Debasmita, Mar 14, 2012.

  1. Debasmita

    Debasmita New Member

    Joined:
    Mar 13, 2012
    Messages:
    3
    Likes Received:
    3
    Trophy Points:
    0
    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.
    Code:
    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

    [​IMG]
     
    Last edited by a moderator: Jan 21, 2017
  2. Ajay12

    Ajay12 New Member

    Joined:
    May 7, 2012
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India, Agartala, Tripura(W)
    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.
     
  3. mfred90

    mfred90 Banned

    Joined:
    Mar 28, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. icool

    icool New Member

    Joined:
    Jan 2, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Java Developer
    Home Page:
    http://fruzenshtein.com/
    Very informative post.
    The table with access modifiers is very useful.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice