![]() |
VOOZH | about |
Modifiers are specific keywords present in Java using that we can make changes to the characteristics of a variable, method, or class and limit its scope. Java programming language has a rich set of Modifiers. Modifiers in Java are divided into two types
Non-access modifiers provide information about the characteristics of a class, method, or variable to the JVM. Seven types of Non-Access modifiers are present in Java. They are
Read more about Non-Access Modifiers in Java. In this article, we are going to discuss the differences among Final, Static, and Abstract Non-Access Modifiers.
The final non-access modifier is applicable to classes, methods, and variables. If we declare a parent class method as final then we canโt 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 y 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) and polymorphism (Because of the final method) hence if there are no specific requirements then it is not recommended to use the final keyword.
Example 1:
Output:
GFG
Example 2:
Output:
๐ OutputThe static non-access modifier is applicable for methods and variables but not for classes. We can not declare a top-level class with a static modifier but we can declare the inner class as static (such types of inner classes are known as static nested classes). In the case of instance variable for every object, a separate copy will be created but in the case of static variable, a single copy will be created at class level and shared by every object of that class.
Example:
Output:
๐ OutputThe abstract non-access modifier is applicable only for classes and methods but not for variables. If we declare any method as abstract then that method must have its implementation in the child class of the respective class because abstract methods never talk about implementation. If any modifier talks about implementation then it forms an illegal combination with an abstract modifier. In a similar way if for any java class if we are not allowed to create an object (because of partial implementation) then such type of class we have to declare with abstract modifier.
Example:
Output:
๐ Output| Final Non-Access Modifier | Static Non-Access Modifier | Abstract Non-Access Modifier |
|---|---|---|
| This modifier is applicable to both outer and inner classes. | This modifier is not applicable to outer classes. | This modifier is applicable to both outer and inner classes. |
| This modifier is not applicable to interfaces | This modifier is not applicable to interfaces. | This modifier is applicable to interfaces. |
| This modifier is the only modifier that is applicable for local variables. | This modifier is not applicable for local variables. | This modifier is not applicable for local variables. |
| Final method can't be inherited. | Static methods can only access the static members of the class and can only be called by other static methods. | Abstract method can be inherited. |