• August 26, 2014
  • Core Java
  • Comments Off on Access modifier in java

Access modifier in java

Access modifiers:

Access modifiers are keywords used for defining accessibility of classes, methods and data members.

Types of access modifier.

  1. 1. Private.
  2. 2. Default
  3. 3. Protected
  4. 4. Public

Private:

Data members, methods and constructors that are declared with private access modifier can be accessed into that class only.

Example:

PrivateAccessModifier.java

/**
 * This program is used to show that private members
 * of a class can be accessed in that class only
 * @author javawithease
 */
class Student {
      //private members of the class
      private int rollNo = 5;
 
      private void showRollNo(){
              //rollNo which a private data member is
              //accessible in that class.
              System.out.println("RollNo = " + rollNo);
      }
}
 
public class PrivateAccessModifier {
       public static void main(String args[]){
              //creating Student class object
              Student obj = new Student();
 
              //compile time error because private members
              //of a class can be accessed in that class only.
              System.out.println(obj.rollNo);
 
              obj.showRollNo();
       }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problems:
The field Student.rollNo is not visible
The method showRollNo() from the type Student is not visible
at com.javawithease.business.PrivateAccessModifier.main
(PrivateAccessModifier.java:24)

Download this example.

Note: A class can have a private constructor but we cannot create an instance of that class from outside the class.

PrivateConstructor.java

/**
 * This program is used to show that we cannot create an instance
 * of that class from outside the class if constructor is private.
 * @author javawithease
 */
class Student {
      //private constructor of the class
      private Student(){                               
 
      }
 
      public void show(){
             System.out.println("Hello javawithease.com");
      }
}
 
public class PrivateConstructor {
       public static void main(String args[]){
               //compile time error in creating Student class object
               //because of private constructor.
               Student obj = new Student();
       }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The constructor Student() is not visible
at com.javawithease.business.PrivateConstructor.main
(PrivateConstructor.java:22)

Download this example.

Note: Classes and interfaces can’t be private except nested classes.

Default:

Classes, data members, methods and constructors that are not declared with any access modifier are treated as default. They can be accessed into all classes within the same package only.

Example 1:

DefaultAccessModifier1.java

/**
 * This program is used to show that members of the class
 * of a class can be accessed in the package level only.
 * @author javawithease
 */
class Student {
      //private members of the class
      int rollNo = 5;
 
      void showRollNo(){
           System.out.println("RollNo = " + rollNo);
      }
}
 
public class DefaultAccessModifier1 {
        public static void main(String args[]){
               //creating Student class object
               Student obj = new Student();
 
               //No compile time error because members of the class
               //of a class can be accessed in that package but can't be
               //accessed outside the package.
               System.out.println(obj.rollNo);
               obj.showRollNo();
         }
}

Output:

5
RollNo = 5

Download this example.

Example 2:

Display.java

package com.javawithease.display;
/**
 * This class is used to print a line.
 * @author javawithease
 */
class Display {
      void display(){
           System.out.println("Hello javawithease.com");
      }
}

Test.java

package com.javawithease.test;
import com.javawithease.display.*;
/**
 * This program is used to show that default members of the class
 * are not accessible from outside the package.
 * @author javawithease
 */
public class Test {
        public static void main(String args[]){
               //compile time error because default members of the class
               //are not accessible from outside the package.
               Display obj = new Display();
               obj.display();
       }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problems:
The type Display is not visible
The type Display is not visible
The type Display is not visible
at com.javawithease.test.Test.main(Test.java:13)

Download this example.

Protected:

Data members, methods and constructors that are declared with protected access modifier can be accessed into all classes within the same package and only in subclasses outside the package.

Example 1:

Display.java

package com.javawithease.display;
/**
 * This class is used to print a line.
 * @author javawithease
 */
public class Display {
       protected void display(){
                 System.out.println("Hello javawithease.com");
        }
}

Test.java

package com.javawithease.display;
package com.javawithease.test;
 
import com.javawithease.display.*;
/**
 * This program is used to show that protected members of the
 * class are accessible from outside the package in subclasses.
 * @author javawithease
 */
public class Test extends Display{
        public static void main(String args[]){
               Test obj = new Test();
 
               //No compile time error because protected members
               //of the class are accessible from outside the
               //package in subclasses.
               obj.display();
         }
}

Output:

Hello javawithease.com

Download this example.

Example 2:

Display.java

package com.javawithease.display;
/**
 * This class is used to print a line.
 * @author javawithease
 */
public class Display {
       protected void display(){
                 System.out.println("Hello javawithease.com");
       }
}

Test.java

package com.javawithease.test;
 
import com.javawithease.display.*;
/**
 * This program is used to show that protected members of the class
 * are not accessible from outside the package in non-subclasses.
 * @author javawithease
 */
public class Test {
       public static void main(String args[]){
              Display obj = new Display();
              //compile time error because protected members
              //of the class are not accessible from outside
              //the package in non-subclasses.
              obj.display();
        }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The method display() from the type Display is not visible
at com.javawithease.test.Test.main(Test.java:15)

Download this example.

Note: Classes and interfaces can’t be protected except nested classes.

 Public:

Classes, data members, methods and constructors that are declared with public access modifier can be accessed everywhere.

Example:

Display.java

package com.javawithease.display;
/**
 * This class is used to print a line.
 * @author javawithease
 */
public class Display {
       public void display(){
              System.out.println("Hello javawithease.com");
       }
}

Test.java

package com.javawithease.test;
 
import com.javawithease.display.*;
/**
 * This program is used to show that public members of the class
 * are accessible everywhere.
 * @author javawithease
 */
public class Test {
       public static void main(String args[]){
              Display obj = new Display();
              //public members of the class accessible everywhere.
              obj.display();
       }
}

Output:

Hello javawithease.com

Download this example.
 
Next Topic: Static import in java with example.
Previous Topic: Package in java with example.

Related Topics:

Package in java with example.
Access modifier in java with example.
Static import in java with example.
Package class in java with example.
this in java with example.
Instance initialize/ Anonymous block in java with example.
super in java with example.
Static in java with example.
final in java with example.

 


Comments are closed.