wellnwill Marathahalli Bangalore

Access Modifiers


Access modifiers represent the visibility or accessibility or scope of class, constructor, method and variable.

There are four types of access modifiers:

  • private
  • default (no modifier)
  • protected
  • public

private access modifier

  • private members can be accessed only within a class.
  • Private members are not inherited to child class.
  • We cannot use private keyword for top level classes.
class Modifier{  
private int a=10;  
private void display()
{
System.out.println("display() of Test");
}  
}  
public class Test {  
 public static void main(String args[]){  
   Modifier m1=new Modifier();  
   System.out.println(m1.a);//Compile Time Exception
   m1.display();// Compile Time Exception
   }  
}  
// O/P  - Compile Time Exception

class Modifier
{  
   private int a=10;  
   private void display()
   {
     System.out.println("display() of Modifier");
   }  
  
   public static void main(String args[]){  
     Modifier m1=new Modifier();  
     System.out.println(m1.a);
     m1.display();
   }  
}  
// O/P - 10, display of Modifier()


default (no modifier)

  • Default modifier is also known as package level access modifier.
  • If we don’t use any modifier, it will be treated as default.
  • Default members can be accessed within class or within package.
  • Default modifier can be used with variable, methods, class and constructor.

package com.pack1;
class Modifier1{  
  int a=10;  
  void display()
  {
    System.out.println("display() of  Modifier1");
  }  
}  
  public class Test1 {  
    public static void main(String args[]){  
    Modifier1 m1=new Modifier1();  
    System.out.println(m1.a);  
    m1.display();
   }  
}  
// O/P - 10,display() of Modifier1

package com.pack1;
class Test
{
  public static void main(String asgs[])
  {
     Modifier1 md1=new Modifier1();
     md1.display();
  }
} 
// O/P - display of modifier1








protected access modifier


  • protected members can be accessible within package and outside a package through inheritance.
  • Protected modifier is not applicable for top level classes. We can apply protected modifier on data member, method and constructor.
  • If we are accessing protected members outside the package through inheritance then we should use that particular child reference only.
  • We can access protected members within the current package anywhere either by using parent reference or by using child reference.
  • We cannot access protected members outside the package with parent class reference.

package com.techbrothers.p3;
public class Test11
{  
  protected void display()
   {
       System.out.println("display() in p3");
   }

      public static void main(String args[]){	
        System.out.println("Hello package p3");
      }
}


package com.techbrothers.p4;
import com.techbrothers.p3.*;
class Test12 extends Test11
{  
   public static void main(String args[]){	
	Test12 t1=new Test12();
	t1.display();
        System.out.println("Hello package p4");
    }
}
// compile- javac –d . Test11.java
           javac  -d   Test12.java
Run - java com.techbrothers.p4.Test12
// O/P - display() in p3, hello package p4



package com.techbrothers.p3;
public class Test20
{  
  protected void display()
  {
    System.out.println("dipaly() in p3");
  }
}
class Test21 extends Test20
{
   public static void main(String args[])
   {
        Test20 t20=new Test20();
	t20.display();
	Test21 t21=new Test21();
	t21.display();
	Test20 t22=new Test21();
	t22.display();
	System.out.println("inside package p3");
    }
}
// O/P - display() in P3--- 3times,inside package p3.

package com.techbrothers.p3;
class Test11
{  
   protected void display()
   {
      System.out.println("dipaly() in p3");
   }
} 
public class Test011
{  
  public static void main(String args[]){	
      Test11 t11=new Test11();
      t11.display();
      System.out.println("Hello package p3");
  }
}
// O/P - display() in p3,Hello package p3.
Note- because both classes are present 
in same package so inheritance is not required.
	

	
	
	
package com.techbrothers.p3;

public class Test20
{  
  protected void display()
  {
    System.out.println("display() I,n p3");
  }
}

package com.techbrothers.p4;
import com.techbrothers.p3.*;

class Test22 extends Test20
{
}

class Test23 extends Test22
{
   public static void main(String args[]){
	Test23 t=new Test23();
	t.display();
	System.out.println("inside package p4");
   }
}
// O/P - dipaly() in p3
         inside package p4
Note- No Compile Time Exception because we are using current class 
reference.

package com.techbrothers.p3;

public class Test21 extends Test20
{
	public static void main(String args[]){
	
	  Test20 t20=new Test20();
	  t20.display();
	
	  Test21 t21=new Test21();
	  t21.display();
	
	  Test20 t22=new Test21();
	  t22.display();
	
	  System.out.println("inside package p3");
    }
}
// O/P - display() in P3---3times, inside package p3.
Note- in this case, we can use any class reference 
because classes are present in same package.

package com.techbrothers.p3;

public class Test20
{  
  protected void display()
  {
    System.out.println("display() in p3");
  }
}











package com.techbrothers.p4;
import com.techbrothers.p3.*;

class Test22 extends Test20
{
}

class Test23 extends Test22
{
   public static void main(String args[]){
	
	Test22 t=new Test22();
	t.display();
	
	System.out.println("inside package p4");
    }
}
// O/P - Compile Time Exception
// we must use current class reference to call proteCompile Time Exceptiond 
members of a class which is present in different package

public modifier

  • public members can be accessed from anywhere.
  • public modifier can be used with variable, methods, class and constructor.
Footer with Map