Tuesday, April 30, 2019

Access Modifiers in Java


In Java Programming, we have Four Access Modifiers, they are:
1.Default
2.Private
3.Public
4.Protected

Default Access Modifier:


If we don’t mention any access modifier means it is a default access modifier, this modifier is can’t be accessed by any other package, and it can be accessed within the package only. The classes which present in the program Core Java Training in Chennai only access this modifier, The outside package access is restricted to this default modifier.Likewise, method and member can be accessed within the class only

Example of Default Access Modifier:


package vbnpackage;

public class Girl {
  /* Since we didn't mention any access modifier here, it would
   * be considered as default.
   */
  int add(int c, int d){
return c+d;
  }
}

Test1.java
package poipackage;

/* We are importing the vbnpackage
* but still we will get error because the
* class we are trying to use has default access
* modifier.
*/
import vbnpackage.*;
public class Test1 {
  public static void main(String args[]){
Girl obj = new Girl();
       /* It will throw error because we are trying to access
        * the default method in another package
        */
obj.add(10, 21);
  }
}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method addTwoNumbers(int, int) from the type Add is not visible
at poipackage.Test1.main(Test1.java:12)

Private Access Modifier:


Private Access Modifier is similar to Default Access Modifier, The scope of this private modifier is limited to the class only. The name itself Java Training Institute in Chennai we can understand this private access modifier, “Private” the data members and packages, methods are only accessed within the class. It cannot create the object to constructor from outside of the class.

Example of Private Access Modifier:

class DEF{  
  private double num1 = 100;
  private int squ(int a){
return a*a;
  }
}  
public class Exp{
  public static void main(String args[]){  
DEF obj = new DEF();  
System.out.println(obj.num1);
System.out.println(obj.squ(10));
  }
}

Output:
Compile - time error

Protected Access Modifier

Protected Access Modifier Java Training Institute in Chennai is quite different, Protected data member and method are only accessed by the class of same package and subclasses present in any package.

Example of Protected Access Modifier:

package asdpackage;
public class Addition {

  protected int add1(int l, int k){
return l+k;
  }
}

Test2.java
package ghtpackage;
import asdpackage.*;
class Test2 extends Addition{
  public static void main(String args[]){
Test2 obj = new Test2();
System.out.println(obj.add1(11, 22));
  }
}

Output:
33

Public Access modifier

Public access modifier can be accessed within and outside the classes , it allow all the package and data member to access the class.Core Java Training in Chennai

Example of Public Access Modifier:

package zxcpackage;

public class Addition1 {

  public int add2(int t, int y){
return t+y;
  }
}

Test3.java
package dsapackage;
import zxcpackage.*;
class Test3{
  public static void main(String args[]){
     Addition1 obj = new Addition1();
     System.out.println(obj.add2(100, 1));
  }
}

Output:
101

No comments:

Post a Comment