Protected vs Final Access Modifier in Java


The protected and final access modifiers determine how a member of a class or method can be accessed. The modifiers are attached to the members at the time of declaration. We know that these access modifiers play a major role in Java oops concepts like encapsulation and inheritance. It helps in preventing misuse of functionalities provided by a member. We will try to understand protected and final access modifiers in Java through example programs.

Access Modifiers in Java

Protected Access Modifier

It is mostly used in the case of inheritance to control the access of parent class members and corresponding child class members. It allows an element to be accessed outside the current package but only to the direct subclasses of classes. Here, packages are the containers that hold a group of classes.

A few examples of protected access modifier −

protected int data1 = 5;
protected double data2 = 5.55;

Here, variables are declared as protected.

Example

The following example illustrates the use of protected method in Java.

class Pack {  
   protected void prnt() {
     String msg = "Tutorials Point!!";
     System.out.print("Welcome to " + msg);
   }  
}  
public class ClassShow extends Pack {  
   public static void main(String args[]) { 
     // creating object of child class
     ClassShow obj = new ClassShow();  
     // method calling through object of child class
     obj.prnt();  
   }  
}  

Output

Welcome to Tutorials Point!!

In the above code, the class ‘Pack’ is parent class of ‘ClassShow’. In the parent class, we have declared a protected method named ‘prnt()’ to print a simple message. In the main() method of child class, we have defined an object of child class ‘ClassShow’ to call the protected method ‘prnt()’.

Final Access Modifier

When any member of a class or method is declared as final using the keyword ‘final’, we basically make them a constant by preventing their modification. The final members must be initialized at the time of declaration or they can also be initialized within constructors. Most of the time we prefer their initialization during declaration.

A few examples of final access modifier −

final int pinCode = 500081;
protected double pi = 3.14;

Here, variables are declared as final.

The final keyword in Java is used in three different ways −

  • final variable − We can’t modify value of a final variable, it is more of a constant.

  • final method − To prevent the overriding, we make a method final. We override a method in the child class to modify the features of a specified method as per needs.

  • final class − We can’t inherit the properties of a final class.

Example

The following example illustrates the use of final keyword with a method in Java.

Code:
class Pack {  
   public final void prnt() {
      String msg = "Tutorials Point!!";
      System.out.print("Welcome to " + msg);
   }
}  
public class ClassShow extends Pack {
   public final void prnt() {
      // overriding prnt method
      String msg = "Tutorix!!";
      System.out.print("Welcome to " + msg);
   }
   public static void main(String args[]) { 
      // creating object of child class
      ClassShow obj = new ClassShow();  
      // method calling through object of child class
      obj.prnt();  
   }  
}  

Output

ClassShow.java:1: error: class, interface, enum, or record expected
Code:
^
1 error

In the above code, the class ‘Pack’ is parent class of ‘ClassShow’. In the parent class, we have declared a final method named ‘prnt()’ to print a simple message. In the child class, we tried to override the ‘prnt()’ method to modify the value of ‘msg’ and inside its main() method, we have defined an object of child class ‘ClassShow’ to call the final method ‘prnt()’. But we got an error that specifies that we can’t override a final method.

Protected vs Final access modifiers

From the above discussion, we can conclude the following differences between Protected and Final access modifiers −

Protected

Final

It is an access modifier means it can control the access of variables and methods.

It is actually a non-access modifier means it can control the behavior of class and its members.

We can’t define a class as protected.

We can define a class as a final.

It is applicable to the member level.

It is applicable to the top level.

We use protected keyword.

It requires final keyword.

The protected members are accessible within the package as well as in other packages. But in the case of other packages, it is accessible to only inherited classes.

The final members can’t be accessed outside the current package.

Conclusion

We started this article by defining protected and final access modifiers and in later sections, we have explained them in detail with respective examples. In the end, we have discussed a few differences between them.

Updated on: 16-May-2023

572 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements