print
Advertisment
Advertisment

Class Based Modifiers

Access modifiers are predefined keywords in Java. Which we use to define the accessibility of class, variable, method and constructor. In this we can change their access level by using access modifiers. Java has class based four modifiers.

Types Of Class Level Access Modifiers

  1. Public
  2. Private
  3. Protected
  4. Default

Public: The public modifier is represented by the public keyword. Using this keyword we can make class, variable, method and constructor public. It gives accessibility to access all those methods, variables & constructor from outside and inside the class. (We can access it from anywhere. There is no restriction of any kind, it has the largest scope as compared to other modifiers. Hence public keyword is most commonly used. However, when we use any public want to access the class.in another package, then in that case we have to import that class.

  • If another programmer uses our class, then we should use the most restrictive access level, we should mostly use the private keyword, (except in special circumstances).
  • We should avoid public fields.

Example

    
   public class Aman{
   public static void main(String[] args) {
   System.out.println("public modifier");
   }
   }
      
      
  

Output :

 
  
    public modifier
    

Advertisment

Private: Methods, data members and constructors declared as private modifiers are accessed in the same class as the class in which they are defined. We cannot access them from outside the class, we cannot declare top level class or interface as private. But we can declare nested classes as private. private is the most restricted modifier i.e. its most controlled scope. But variables that are declared as private can also be accessed outside the class, and this is possible only if there is a public getter() and setter method is available inside the class.

Example

    
   
  private class Example{
  private double d = 55.55f;
  private String address(){
  System.out.println("private area");
  return "address";
  }
  }
  public class Program{
  public static void main(String[] args) {
  Example e = new Example();
  System.out.println(e.address()); //Compile Time Error
  System.out.println(e.d); //Compile Time Error
  System.out.println();
  }
  }
   
  

Output :

 
  
   compile time error
    

Here we made the object of the class program in class Example and wanted to print its variable d and method address(), then we got compile time error, this happened because d and method address() have been declared private inside the calss program. And the scope of private is only till the class in which it is declared.


Protected: Protected access modifiers are commonly used in parent-child relationships. The meaning is done in inheritance. Variables, methods, and constructors that are declared protected are all accessible only by any class located in the same package or by a subclass or child class located in another package. Protected members (variables, methods etc. ) from other packages In the package in which it is not declared, in that case we have to use inheritance.

  • This applies only to variables, methods and constructors. It is not applied to classes and interfaces.
  • If the constructor is declared protected inside a class, then in that case, the instance of that class, that is, the object, can be created only in the package in which that class is located. An object of that class cannot be created in any other package, doing so will result in a compile time error.

Example

    
   creat a program
      
      
  

Output :

 
  
   output
    

Advertisment

Default: When we do not use any access modifier for any class, variable, method & constructor etc. So it is called default access modifier.The default modifier is used within the same package. When we have a class with default modifier in a package. Then only other classes which are inside this package can access this class. No other class outside the package can access this class

Example

    
class DefaultClassExample{
  public static void main(String args[]){
    System.out.println ("Hello, I am Default Class");
  }
}
   
  

In the above example Hello class has default access modifier. And this Hello class can be accessed by all those classes which will be inside the mypackage package. If we want to access this class outside this package then we will get compiler error.

Output :

 
  
   Hello, I am Default Class
  
Advertisment
Advertisment
arrow_upward