Saturday, February 16

Difference between public, private, protected and default



 

  Access Modifier Visibility


Modifier
Class
Package
Subclass
Everywhere
public
Yes
Yes
Yes
Yes
protected
Yes
Yes
Yes
No
default
Yes
Yes
No
No
private
Yes
No
No
No

public  - Members are visible in same class, same package, sub classes and in other classes.

protected - Members are visible in same class, same package, sub classes but not in other classes.

default - Members are visible in same class, same package but not in sub classes and other classes.

private - Members are visible only in same class but not in same package, sub classes and other classes.

  

  Access Modifier Level Applicability


Modifier
Member Field
Member Method
Nested Class
Top Level Class
public
Yes
Yes
Yes
Yes
default
Yes
Yes
Yes
Yes
private
Yes
Yes
Yes
No
protected
Yes
Yes
Yes
No
 
 public, default  - These modifiers can be applied to members of the class and also to the top class. 

 private, protected  - These modifiers can be applied to member of a class only but not to the top class.



Simple Inheritance

parent.java
package r1;
public class parent
{
        int x;
      
       public parent()
       {
              x=10;
       }
             
       public static void main(String args[])
       {
             
              parent p = new parent();
              System.out.println("parent main x="+p.x);
       }
}

Output
parent main x=10


sub1.java
package r1;
public class sub1 extends parent  // a subclass of parent
{
       sub1()
       {
              x=20;
       }
      
       public static void main(String args[])
       {
              sub1 s1 = new sub1();
              System.out.println("In r1 pckg, sub1 class, main fn, sub1.x = "+s1.x);           
       }     
}

Output
In r1 pckg, sub1 class, main fn, sub1.x = 20


Examples of  Access Modifiers

Following are the errors that occurs if access modifiers are not used properly:


private - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        private int x;      // private access modifier
}

sub1.java
package r1;  // same package as the parent
public class sub1 extends parent  // a subclass of parent
{
       sub1()
       {
              x=20;
       }
      
       public static void main(String args[])
       {
              sub1 s1 = new sub1();
              System.out.println("In r1 pckg, sub1 class, main fn, sub1.x = "+s1.x);           
       }     
}
Output
The field parent.x is not visible

Because: sub1.java is not the same class where x is declared.



default - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        int x;  // no access modifier is default
}

sub2.java
// sub2 class is not part of r1 package
import r1.parent;   // so  its imported
public class sub2 extends parent  // a subclass of parent
{
       sub2()
       {
              x=30;
       }
      
       public static void main(String args[])
       {
              sub2 s2 = new sub2();
              System.out.println("In no pckg, sub2 class, main fn, sub2.x = "+s2.x);
       }     
}

Output
The field parent.x is not visible

Because: sub2.java is not in the same package of parent where x is declared.



 protected - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        protected int x;  // protected access modifier
}

some1.java
// some1 class is not part of r1 package
import r1.parent;
public class some1 // not a subclass of parent
{            
       public static void main(String args[])
       {
          parent p = new parent();
          System.out.println("In no pckg, some1 class, main fn, parent.x = "+p.x);
       }     
}

Output
The field parent.x is not visible

Because: some1.java is neither in the same package nor in subclass of parent  where x is declared.