Private and Final Methods in Java
Last Updated :
24 Apr, 2025
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.
Private vs Final Method
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. |
---|
Private Methods in Java
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:
- Restriction: Private methods are restricted from being called by other methods directly.
- Encapsulation: Private methods allow the implementation details to be hidden from other classes, which is useful for data hiding and provides security against unauthorised modification.
- Exposure Prevention: Private methods are used for internal operations, which are used for internal calculations or tasks that prevent exposing the internal calculation to other classes.
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
Java
// Java program demonstrating the use of a private method
// and accessing it within the same class
class Geeks {
// A private method
private void sayHello() {
System.out.println("This is a private method in the Geeks class.");
}
public static void main(String[] args) {
// Object of Geeks class
Geeks obj = new Geeks();
// Accessing the private method
// within the same class
obj.sayHello();
}
}
OutputThis 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.
Java
// Java program demonstrating the use of a private method
// and accessing it within the same class
class Greet{
// A private method
private void sayHello() {
System.out.println("This is a private method in the Geeks class.");
}
}
public class Geeks {
public static void main(String[] args) {
Greet obj = new Greet();
// Accessing the private method
// from another class
obj.sayHello();
}
}
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.
Final Methods in Java
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:
- Restrict Overriding: A final method cannot be overridden by any of the subclasses (child classes).
- Consistency: The final methods ensure that the method behaviour remains the same and unmodified.
- Prevent from Critical Operations: Final methods are useful for preventing the critical methods modification where overriding can result in undefined behaviour.
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.
Java
// Java program demonstrating the use of a final method in a class
// and trying to override the final method
class Greet {
// A final method
public final void sayHello() {
System.out.println("Hello from the Greet class!");
}
}
class ExtendedGreet extends Greet {
// Trying to override the final method
public void sayHello() {
System.out.println("Hello from the ExtendedGreet class!");
}
}
public class Geeks {
public static void main(String[] args) {
Greet obj = new Greet();
obj.sayHello();
}
}
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.
Similar Reads
Java Methods Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
8 min read
Parameter Passing Techniques in Java with Examples There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the "caller function" and B is called the "called function or callee function". Also, the arguments wh
4 min read
Java is Strictly Pass by Value! In order to understand more of how the java is processing the parameter in methods and functions, lets compare the java program with a C++ code which would make it more clear and helps you get the major difference between how the parameters are being passed to any methods or functions wrt passing pa
6 min read
How are parameters passed in Java? See this for detailed description. In Java, parameters are always passed by value. For example, following program prints i = 10, j = 20. java // Test.java public class Test { // swap() doesn't swap i and j public static void swap(Integer i, Integer j) { Integer temp = new Integer(i); i = j; j = temp
1 min read
Method overloading and null error in Java In Java it is very common to overload methods. Below is an interesting Java program. Java public class Test { // Overloaded methods public void fun(Integer i) { System.out.println("fun(Integer ) "); } public void fun(String name) { System.out.println("fun(String ) &quo
2 min read
Can we Overload or Override static methods in java ? Let us first define Overloading and Overriding. Overriding: Overriding is a feature of OOP languages like Java that is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in the superclass (or base class). The implementation to be executed i
5 min read
Access specifier of methods in interfaces In Java, all methods in an interface are public even if we do not specify public with method names. Also, data fields are public static final even if we do not mention it with fields names. Therefore, data fields must be initialized. Consider the following example, x is by default public static fina
1 min read
Java main() Method - public static void main(String[] args) Java's main() method is the starting point from where the JVM starts the execution of a Java program. JVM will not execute the code if the program is missing the main method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very important.The Java co
6 min read
Is main method compulsory in Java? The answer to this question depends on the version of java you are using. Prior to JDK 7, the main method was not mandatory in a java program. You could write your full code under static block and it ran normally. The static block is first executed as soon as the class is loaded before the main(); t
2 min read
Understanding "static" in "public static void main" in Java Following points explain what is "static" in the main() method: main() method: The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method. Static is a keyword. The role of adding static before an
3 min read