Protected vs Final Access Modifier in Java
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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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.
GFG GFG GFG
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 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.
- 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 vece-versa holds true that means a non-final class can inherited final class methods.
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.