« Previous Page Next Page »
    

Access and Non-access Modifiers types in Java.

POSTED

AUTHOR: VARUN SINGH

A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. There are two types of modifiers in java: access modifiers and non-access modifiers. Access Modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member.


There are 4 types of java access modifiers:

  • 1. default: visible to the package
  • 2. private: visible to the class only
  • 3. protected: visible to the package and all subclasses
  • 4. public: visible to the world

  • There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.


    Default Access Modifier:

    When no access modifier is specified for a class, method or data member are known as Default Access Modifier. The data members, class or methods don't have any access modifiers, they are accessible only within the same package.

    In this example, we will create two packages p1 and p2 and the classes in the packages will be having the default access modifiers. And we will trying to access class of package p1 in package p2.

    File: Coderglass.java

    //Java program to illustrate default modifier
    package p1;
     
    //Class Coderglass is having Default access modifier
    class Coderglass
    {
        void display()
           {
               System.out.println("Coderglass Tech Blog.");
           }
    }
    

    File: Coderglass2.java

    //Java program to illustrate error while 
    //using class from different package with default modifier
    
    package p2;
    import p1.*;
     
    //This class is having default access modifier
    class Coderglass2
    {
        public static void main(String args[])
           {  
              //accessing class Coderglass from package p1
    
              Coderglass coder = new Coderglass();
     
              coder.display();
           }
    }
    


    Tips: How to compile java file to create package:

    Compile: -d is used to create package in the current directory.

      javac -d . Coderglass.java
      javac -d . Coderglass2.java
    

    Run: To run java file use package name with java file.

      java p2.Coderglass2
    

    Output:

    Compile time error
    



    Private Access Modifier:

    • The private access modifier is specified using the keyword private.

    • The methods or data members declared as private are accessible only within the      class in which they are declared.

    • Any other class of same package will not be able to access these members.

    • Classes or interface can not be declared as private.

    In this example, we will create two classes Coderglass3 and Coderglass4 within same package p1. We will declare a method in Coderglass3 as private and try to access this method from class Coderglass4 and see the result.

    File: Coderglass4.java

    //Java program to illustrate error while 
    //using class from different package with private modifier
    
    package p1;
     
    class Coderglass3
    {
       private void display()
        {
            System.out.println("Coderglass is the best programming blog.");
        }
    }
     
    class Coderglass4
    {
       public static void main(String args[])
          {
             Coderglass3 coder = new Coderglass3();
             
              //trying to access private method of another class
              
              coder.display();
          }
    }
    

    Output

    error: display() has private access in Coderglass3
              coder.display();
                   ^
    


    Protected Access Modifier:

    • The protected access modifier is specified using the keyword protected.

    • The methods or data members declared as protected are accessible within same     package or sub classes in different package.

    In this example, we will create two packages p1 and p2. Class Coderglass5 in p1 is made public, to access it in p2. The method display in class Coderglass5 is protected and class Coderglass6 is inherited class A by creating an object of Coderglass6.

    File: Coderglass5.java

    //Java program to illustrate protected modifier
    package p1;
     
    //Class Coderglass5
    public class Coderglass5
    {
       protected void display()
        {
            System.out.println("Coderglass programming blog.");
        }
    }
    

    File: Coderglass6.java

    //Java program to illustrate protected modifier
    package p2;
    import p1.*; //importing all classes in package p1
     
    //Class Coderglass6 is subclass of Coderglass5
    class Coderglass6 extends Coderglass5
    {
       public static void main(String args[])
       {  
           Coderglass6 coder = new Coderglass6();  
           coder.display();  
       }  
         
    }
    

    Compile: -d is used to create package in the current directory.

      javac -d . Coderglass5.java
      javac -d . Coderglass6.java
    

    Run: To run java file use package name with java file.

      java p2.Coderglass6
    

    Output:

    Coderglass programming blog.
    

    Public Access Modifier:

    • The public access modifier is specified using the keyword public.

    • The public access modifier has the widest scope among all other access modifiers.

    • If Classes, methods or data members declared as public are accessible from every where.

    • There is no restriction on the scope of a public data members.

    File: Coderglass1.java:

    //save by Coderglass1.java  
      
    package p1;  
    public class Coderglass1{  
    public void display()
     {
       System.out.println("Hello Coderglass");
     }  
    }
    

    File: Coderglass2.java:

    //save by Coderglass2.java  
      
    package p2;  
    import p1.*;  
      
    class Coderglass2{  
      public static void main(String args[]){  
       Coderglass1 coder = new Coderglass1();  
       coder.display();  
      }  
    }
    
    

    Output:

    Hello Coderglass
    


    The End:

    Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

    If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

    « Previous Page Next Page »

    Comments: