Open In App

Protected vs Final Access Modifier in Java

Last Updated : 11 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access modifiers. So access modifiers are used to set the accessibility of classes, methods, and other members.

Protected Access Modifier

This modifier can be applied to the data member, method, and constructor, but this modifier can’t be applied to the top-level classes and interface. A member is declared as protected as we can access that member only within the current package but only in the child class of the outside package.

Example

Java




// Java Program to illustrate Protected Access Modifier
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Class 1
// Declaring a parent class A
class A {
 
    // Declaring a protected method m1()
    protected void m1() { System.out.println("GFG"); }
}
 
// Class 2
// Creating a child class by extending the class A
class B extends A {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of parent class
        // using parent reference
        A a = new A();
 
        /// Calling method m1
        a.m1();
 
        // Creating an object of child class
        // using child reference
        B b = new B();
 
        // Calling method m1
        b.m1();
 
        // Creating an object of child class
        // using parent reference
        A a1 = new B();
 
        // Calling m1 method
        a1.m1();
    }
}


Output

GFG
GFG
GFG

Output explanation: 

Here we have created 3 objects using parent reference and child reference and call m1() method on it, and it successfully executed so from the above example we can say that we can access the protected method within the current package anywhere either by using parent reference or by child reference.
 

Final Access Modifier

It is a modifier applicable to classes, methods, and variables. Basically it refers to specimen that it can  not be changed in future once initialized. Suppose if we declare a parent class method as final then we can not override that method in the child class because its implementation is final and if a class is declared as final we can’t extend the functionality of that class i.e we can’t create a child class for that class i.e inheritance is not possible for final classes. Every method present inside the final class is always final by default but every variable present inside the final class need not be final. The main advantage of the final keyword is we can achieve security and we can provide a unique implementation. But the main disadvantage of the final keyword is we are missing key benefits of OOPs like Inheritance(Because of the final class), Polymorphism(Because of the final method)  hence if there are no specific requirements then it is not recommended to use the final keyword. 

In simpler words it is simple a modifier that is used to create final method, variables whatever we want to create in our program as per requirement. We use where we want to make part of the program be it ranging from smallest be variable to bigger chunk be it method consider for date of birth.

final int dob = 25011947 ;

Now you must be wondering what we simply declare and not initialize than in this case remember value can be initialized later on with the help of constructors only and these constructors can  not be inherited. 

Note: 

  • If the variable is declared static alongside final  as shown below then it can only be initialized in the static blocks only.
public static final weekDay ;
  • In case of class if declare as final than it can not be implemented  or inherited  by other non-final class but vice-versa holds true that means a non-final class can inherited final class methods.

Example

Java




// Java program to Illustrate Final keyword
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Class 1
// Declaring parent class P
class P {
 
    // Declaring a first name method
    public void firstName()
    {
 
        // Display firstname
        System.out.println("Rahul ");
    }
 
    // Declaring a final surName method
    public final void surName()
    {
 
        // Display surname
        System.out.println("Trivedi");
    }
}
 
// Class 2
// Creating a child class of above parent class
class C extends P {
 
    // Method 1
    // Overriding the surName() method
    public void surName()
    {
 
        // Display surname
        System.out.println("Sharma");
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Display message
        System.out.println("GFG");
    }
}


Output:

 

Now let us discuss the differences between these two which are conclusively listed below in tabular format as provided below:

                              Protected Access Modifier                          Final Access Modifier
This modifier is not applicable to top-level classes. This modifier is applicable to top-level classes. 
This modifier is applicable to interfaces. This modifier is not  applicable to interfaces 
This modifier is applicable to both the enum and constructor. The final modifier is not applicable to both the enum and constructor. 
This modifier is not applicable for local variables. This modifier is the only modifier that is applicable for local variables. 
We can access protected members from the outside package through child reference. We cannot access final members from the outside package.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads