![]() |
VOOZH | about |
Methods in Java play an important role in making the code more readable, support code reusability, and defining the behaviour of the objects. And we can also restrict the methods based on requirements using the keywords and modifiers such as final and private. These two have different, distinct purposes in controlling. In this article, we are covering the use cases and their significant differences.
Now, first see the key differences between private and final methods in Java.
The private and final are both method modifiers, and they have different use cases, and their common differences are mentioned below:
Aspect | Private Methods | Final Methods |
|---|---|---|
Visibility | This method is only accessible within the same class. | This method can be accessed from another class based on the modifiers such as public, private and protected. |
Inherited | Private methods cannot be accessed in a subclass. | It can be inherited, but the final method cannot be overridden in a subclass. |
Purpose | Used to encapsulate the methods that should not be accessed directly. | Used to prevent the method from any kind of modification in subclasses and help to provide consistency. |
Access | It has limited access only to the class where it is defined. | It depends on the modifiers, same as non-final methods. |
The private modifier in Java is used to restrict the access level if a method is marked with the private keyword, then it can only be accessed within the class in which it is defined and when a method is declared as private then it cannot be directly called from the outside of the class, not even by the child or subclass or other classes in the same package. It is very useful in many situations, such as providing the abstraction (showing the necessary details and hiding the implementation).
Important Characteristics of Private Methods:
Note: If we try to directly access the private method from another class, then it will result in a compile-time error.
Example 1: Demonstration of Private Methods in Java
This is a private method in the Geeks class.
Explanation: In the above example, we use the private method and call it from the same class by creating an object of the Geeks class, and it will print the message.
Example 2: Attempt to access the private method of another class.
Output:
Explanation: In the above example, we try to access the private method of another class, and it will throw a compile-time error as shown in the output image.
If the method is declared using the final keyword in Java, then the method cannot be overridden by the child or subclass. It is useful when we want to restrict that the other class from modifying the behaviour of the method and prevent the method from altering critical functionality that should remain consistent across different subclasses.
Important Characteristics of Final Methods:
Note: If we try to override the final method in subclass then it throw the compile-time error.
Example: Attempt to override (modify) the final method in a subclass.
Output:
Explanation: In the above example, we create Greet class and declare a final method sayHello() and then create another class ExtendedGreet, and inherit the properties from Greet class through inheritance and try to modify the final method which will result in compile-time error as shown in the output image.