Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Access Modifiers

• In Java Programming Language we use Access Modifiers to set the scope of members like variables, methods, and constructors.
• In Java we have four different types of Access Modifiers: -

a. Public
b. Protected
c. Default
d. Private

Public Access Modifier: -

a. public is a keyword in Java.
b. When we declare a member along with public access modifier then we can access that member from:

o The same class
o Different classes of same package using their class objects
o Different classes of different packages using inheritance concept
o Outside the package, more easily, using object as well as inheritance method.

c. Public Access Modifier has highest access privilege in Java.

Protected Access Modifier: -

a. protected is a keyword in Java.
b. In Java program we can access a protected member from the same class and from different classes of same package using both object and inheritance concept; however, we cannot access a protected member from outside the package using objects. In order to access a protected member from outside the package, we have to use inheritance concept.

Default Access Modifier: -

a. default is not a keyword in Java as Access Modifier.
b. When we declare any member (like variable, method etc.) inside the class without any access modifier then the access modifier gets included by default which is default but we cannot write default explicitly, otherwise we will get an error at compile time.
c. In Java we can directly access a default member from same class or from different classes of same package using object and inheritance, but we don’t have a way to access that from outside the package.

Private Access Modifier: -

a. private is a Java Keyword so always the word must be entered entirely in lower case in the program shell.
b. When we declare a member using private access modifier then that member is called a private member.
c. Private members are not visible from outside their class so they can’t be accessed from outside their class.
d. In Java, private access modifier has lowest privilege among the four access modifiers.

            
package com.P1;

public class A{
	public static int a = 10;
	protected static int b = 20;
	static int c = 30;
	static private int d = 40;

	public static void main(String arg[]){
		System.out.println("main in A class of com.P1 package");
		System.out.println("a :- "+a);
		System.out.println("b :- "+b);
		System.out.println("c :- "+c);
		System.out.println("d :- "+d);
	}
}
    main in A class of com.P1 package
    a :- 10
    b :- 20
    c :- 30
    d :- 40
            

In the above example we can see that we have class A available in com.P1 package and in it we have declared class level variable along with all the access modifiers. In the same class we have a main method from where we are accessing all the variables directly; so it proves that we can access public, protected, default and private members from the same class itself more easily.

            
package com.P1;
 
 public class A{
     public int a = 10;
     protected int b = 20;
     int c = 30;
     private int d = 40;
 }
 
            
 package com.P1;
 
 public class B{
     
     public static void main(String arg[]){
         System.out.println("main in B class of com.P1 package");
         A a1 = new A();
         System.out.println("a :- "+a1.a);
         System.out.println("b :- "+a1.b);
         System.out.println("c :- "+a1.c);
         // System.out.println("d :- "+a1.d);
     }
 }
    main in B class of com.P1 package
    a :- 10
    b :- 20
    c :- 30
            

In the above example we can see that class B is a different class in com.P1 package. As per the first section of the example we have class A containing variables with different access modifiers and, as you can see, class B is available in the same package com.P1; using class A object we are able to access from class B easily; however, as we know that we can’t access a private member from outside its class, and hence we can’t access private int d = 40 from the other class B.

            
package com.P1;
 
 public class A{
     public static int a = 10;
     protected static int b = 20;
     static int c = 30;
     private static int d = 40;
 }
  
            
 package com.P1;
 
 public class B extends A{
     
     public static void main(String arg[]){
         System.out.println("main in B class of com.P1 package");
         System.out.println("a :- "+a);
         System.out.println("b :- "+b);
         System.out.println("c :- "+c);
         // System.out.println("d :- "+d);
     }
 }
    main in B class of com.P1 package
    a :- 10
    b :- 20
    c :- 30
            

In the above example we can see that class B is a different class in com.P1 package. In the first section of the example, we have class A containing static variables with different access modifiers. As you can see that class B, which is the sub class of class A, is available in the same package i.e., com. P1. In the B class main method we are able to easily access the public, protected and default members of class A directly but private member of class A is not accessible from class B since private members are not visible from outside their classes.

            
package com.P1;
 
 public class A{
     public static int a = 10;
     protected static int b = 20;
     static int c = 30;
     private static int d = 40;
 }
 
            
 package com.P1;
 
 public class B extends A{
     
     public static void main(String arg[]){
         System.out.println("main in B class of com.P1 package");
         System.out.println("a :- "+a);
         System.out.println("b :- "+b);
         System.out.println("c :- "+c);
         // System.out.println("d :- "+d);
     }
 }
 
        
    main in B class of com.P1 package
    a :- 10
    b :- 20
    c :- 30
            

In the above example we can see that class B is a different class in com.P1 package. In the first section of the example, we have class A containing static variables with different access modifiers. As you can see that class B, which is the sub class of class A, is available in the same package com.P1. In B class main method we are easily able to directly access the public, protected and default members of class A except private member as private members are not visible from outside their class and class B is outside class A.

            
package com.P1;
 
 public class A{
     public int a = 10;
     protected int b = 20;
 int c = 30;
     private int d = 40;
 }
  
            
 package com.P2;
 import com.P1.A;
 
 public class B {
     
     public static void main(String arg[]){
         System.out.println("main in B class of com.P1 package");
         A a1 = new A();
         System.out.println("a :- "+a1.a);
         // System.out.println("b :- "+a1.b);
         // System.out.println("c :- "+a1.c);
         // System.out.println("d :- "+a1.d);
     }
 }
        
    main in B class of com.P1 package
    a :- 10
            

In this example we have two different classes A and B available in two different packages com.P1 and com.P2 respectively. In class B we are accessing the values of all the variables of class A using class A object and we can see that only public members of class A can be accessed from outside their package using the object.

            
package com.P1;
 
 public class A{
     public static int a = 10;
     protected static int b = 20;
     static int c = 30;
     private static int d = 40;
 }
 
            
 package com.P2;
 import com.P1.A;
 
 public class B extends A{
     
     public static void main(String arg[]){
         System.out.println("main in B class of com.P1 package");
         System.out.println("a :- "+a);
         System.out.println("b :- "+b);
         // System.out.println("c :- "+c);
         // System.out.println("d :- "+d);
     }
 }
        
    main in B class of com.P1 package
    a :- 10
    b :- 20
            

In this example we have two different classes A and B available in two different packages that are com.P1 and com.P2 respectively. In class B, which is the sub class of A, we are accessing the values of all the variables of class A using inheritance concept and we can see that only public and protected members of class A can be accessed from outside their package.