Modifiers & its types



  • Public, Private and Protected is keywords.
  • These keywords are called access modifiers. 
  • An access modifier restricts the access of a class, constructor, data member and method in another class. 
  • In java we have four access modifiers:
    • 1. default
    • 2. private
    • 3. protected
    • 4. public

1. Default access modifier

  • When we do not mention any access modifier, it is called default access modifier. The scope of this modifier is limited to the package only. 
  • Example:
    • A class with the default access modifier in a package.
    • So, those classes that are in this package can access this class. 
    • No other class outside this package can access this class. 
  • Example:
    • Similarly, a default method or data member in a class, it would not be visible in the class of another package. 
  • Full Example:

Addition.java
package abcpackage;
public class Addition {
   int addTwoNumbers(int a, int b){
return a+b;
   }
}

Test.java

package xyzpackage;
import abcpackage.*;
public class Test {
   public static void main(String args[]){
Addition obj = new Addition();
        /* It will throw error */
obj.addTwoNumbers(10, 21);
   }
}

2. Private access modifier

  • The scope of private modifier is limited to the class only.
  • Class and Interface cannot be declared as private.
  • If a class has private constructor then you cannot create the object of that class from outside of the class.
  • Full Example:

class ABC{ 
   private double num = 100;
   private int square(int a){
return a*a;
   }

public class Example{
   public static void main(String args[]){ 
ABC obj = new ABC(); 
System.out.println(obj.num);
System.out.println(obj.square(10));
//Compile - time error
   } 
}

3. Protected Access Modifier

  • Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package. 
  • Classes cannot be declared protected. 
  • This access modifier is generally used in a parent child relationship.

Full Example:
Addition.java

package abcpackage;
public class Addition {

   protected int addTwoNumbers(int a, int b){
return a+b;
   }
}

Test.java

package xyzpackage;
import abcpackage.*;
class Test extends Addition{
   public static void main(String args[]){
Test obj = new Test();
System.out.println(obj.addTwoNumbers(11, 22));
   }
}

4. Public access modifier

  • The members, methods and classes that are declared public can be accessed from anywhere. 
  • This modifier doesn’t put any restriction on the access.
  • Full Example:


Addition.java
package abcpackage;
public class Addition {

   public int addTwoNumbers(int a, int b){
return a+b;
   }
}

Test.java
package xyzpackage;
import abcpackage.*;
class Test{
   public static void main(String args[]){
      Addition obj = new Addition();
      System.out.println(obj.addTwoNumbers(100, 1));
   }
}
Difference between:

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form