Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Access modifiers

Sat Feb 14, 2009 7:54 pm

Access modifiers:
Access modifiers in Java determine the visibility and the scope of the Java elements. You can use access modifiers on a class and on its members .if you didn't specify a modifier it will be sit to the default modifier. There are there access modifiers in Java and they are (public, protected, and private) in addition to the default modifier which is enforced when you don't determine the modifier.

The public modifier :

Public modifier achieves the highest level of accessibility. The most know public method in Java is the main function. It is public so all Java run times can invoke it .when you define your class as a public it then can be used in anywhere in your application and same to the public members . For example you can make a member variable as a public and access it directly using the class instance and the same to public methods. For example :

Code:
public class Accessmodifiers {
       
    public String text="text";

    public String getText() {
        return text;
    }
   
   
       public static void main(String[] args) {
             Accessmodifiers accessmodifiers=new Accessmodifiers();
             System.out.println("public instance variables : "+accessmodifiers.text);
             System.out.println("public functions : "+accessmodifiers.getText());             
    }
}


The output is :
Code:
public instance variables : text
public functions : text

As you can see you can just accesses the public members using class instances.

It is not a good object oriented programming technique to define instance variables as public, (public variables can be changed from anywhere) instead you should access them using public functions to make more checking and logic on them.

The private modifier :
Private modifier achieves the lowest level of accessibility. You can apply private modifier to variables, methods and inner classes and you can't apply it to top level class. Private members can only be accessed from inside the class .Subclass can't invoke the private members of the base class. Example:
Code:
public class Accessmodifiers {
       
    private String text="text";

    public String getText() {
        return text;
    }
   
   
       public static void main(String[] args) {
              subAccessmodifiers accessmodifiers=new  subAccessmodifiers();
              //error (access of private member)
             System.out.println("public instance variables : "+accessmodifiers.text);
             System.out.println("public functions : "+accessmodifiers.getText());             
    }
}
class  subAccessmodifiers extends Accessmodifiers
{
    public String subGetText()
    {
        //error (access of private member)
        return text;
    }
}


The code above has two error , one in the main function when you try to access private class member using class instance and the other in the subGetText() function of the sub class . As you can see you can't access private members even in child class .We always create a public functions to access the private data variables.

The protected modifier :
Protected modifier achieve the higher level of accessibility than private modifier smaller level accessibility than protected .Child classes can access the base class protected data directly .But you can access the protected members directly from the class instance only if you are in the same package.

Example:
Code:
public class Accessmodifiers {
       
    protected String text="text";

    public String getText() {
        return text;
    }
   
   
       public static void main(String[] args) {
              subAccessmodifiers accessmodifiers=new  subAccessmodifiers();
              //work with no error
             System.out.println("public instance variables : "+accessmodifiers.text);
             System.out.println("public functions : "+accessmodifiers.getText());             
    }
}
class  subAccessmodifiers extends Accessmodifiers
{
    public String subGetText()
    {
        //work with no error 
        return text;
    }
}


As you see above, the errors are gone, when we use the protected modifier for the text variable.

Note: you can't specify a protected modifier for top level class.

The default modifier:
When you don't set access modifier for the element, it will follow the default accessibility level. There is no default modifier keyword .Don’t put any access modifiers and the compiler will consider it as default modifier. Classes, variables, and methods can be default accessed. Using default modifier, classes can be accessed while you are in same package.

Note: The different between the default and protected modifier, in default, to access a member from the child class, the class must be in the same package of the super class. While in the protected case. it can be from out the super class package.
Example :
Code:
public class Accessmodifiers {
       
     String text="text";//Default access modifier

    public String getText() {
        return text;
    }
   
   
       public static void main(String[] args) {
              subAccessmodifiers accessmodifiers=new  subAccessmodifiers();
              //Can be accessed
             System.out.println("public instance variables : "+accessmodifiers.text);
             System.out.println("public functions : "+accessmodifiers.getText());             
    }
}
class  subAccessmodifiers extends Accessmodifiers
{
    public String subGetText()
    {
        //Can be accessed ,
        return text;
    }
}

Before i finish writing about access modifiers ,there is a important point i have to mention .It is about functions overriding and how it is related to access modifiers .The idea that the overridden function can't be less accessible than the original one .for example (public -- > can't be -->protected ) .

In the following table will summarize accessibility of a class member:


----------------------------------------------------------------------------------
Access modifier Class Child Class Package All others
----------------------------------------------------------------------------------
private Yes No No No
protected Yes Yes Yes No
public Yes Yes Yes Yes
Default Yes Yes Yes No
----------------------------------------------------------------------------------



Note : remember the different between protected and default which is mentioned above



Post a reply
  Related Posts  to : Access modifiers
 access modifiers in java     -  
 class is declared without any access modifiers     -  
 Example of using access key with links     -  
 How java access URL     -  
 MS-Access with java     -  
 access element in array C++     -  
 servlet connectivity with ms access     -  
 HOW CONNECT WEB DEVELOPER 2008 TO MS ACCESS     -  
 Restricted protected Access modifier in jsp     -  
 HOW I Access the Parallel Port in Java?     -  

Topic Tags

Java OOP