Introduction
Private methods in interfaces were introduced in Java 9. These methods allow you to define helper methods within an interface that can be used by other default and static methods within the same interface. Private methods help to avoid code duplication and improve code maintainability by enabling the reuse of common logic across multiple methods within the interface.
Key Points:
- Encapsulation: Private methods encapsulate common functionality within the interface.
- Reusability: Enable the reuse of code across multiple default and static methods.
- Maintainability: Improve code maintainability by reducing duplication.
Table of Contents
- Defining Private Methods
- Using Private Methods in Default Methods
- Using Private Methods in Static Methods
- Real-World Example
- Conclusion
1. Defining Private() Methods
Private methods in interfaces can be defined using the private
keyword. They can only be accessed within the interface itself.
Example:
public interface MyInterface {
// Default method
default void defaultMethod() {
System.out.println("Default Method");
commonMethod();
}
// Static method
static void staticMethod() {
System.out.println("Static Method");
commonStaticMethod();
}
// Private method
private void commonMethod() {
System.out.println("Common Method");
}
// Private static method
private static void commonStaticMethod() {
System.out.println("Common Static Method");
}
}
Explanation:
- defaultMethod(): A default method that calls the private method
commonMethod()
. - staticMethod(): A static method that calls the private static method
commonStaticMethod()
. - commonMethod(): A private method that can be used by other default methods within the interface.
- commonStaticMethod(): A private static method that can be used by other static methods within the interface.
2. Using Private() Methods in Default() Methods
Private methods can be used to encapsulate common logic that can be reused by multiple default methods.
Example:
public interface MyInterface {
// Default method 1
default void methodOne() {
System.out.println("Method One");
log("Method One called");
}
// Default method 2
default void methodTwo() {
System.out.println("Method Two");
log("Method Two called");
}
// Private method
private void log(String message) {
System.out.println("Log: " + message);
}
}
Explanation:
- methodOne(): Calls the private method
log()
to log a message. - methodTwo(): Calls the private method
log()
to log a message. - log(): A private method used by
methodOne()
andmethodTwo()
to log messages.
3. Using Private() Methods in Static() Methods
Private static methods can be used to encapsulate common logic that can be reused by multiple static methods.
Example:
public interface MyInterface {
// Static method 1
static void staticMethodOne() {
System.out.println("Static Method One");
logStatic("Static Method One called");
}
// Static method 2
static void staticMethodTwo() {
System.out.println("Static Method Two");
logStatic("Static Method Two called");
}
// Private static method
private static void logStatic(String message) {
System.out.println("Log: " + message);
}
}
Explanation:
- staticMethodOne(): Calls the private static method
logStatic()
to log a message. - staticMethodTwo(): Calls the private static method
logStatic()
to log a message. - logStatic(): A private static method used by
staticMethodOne()
andstaticMethodTwo()
to log messages.
4. Real-World Example
Let’s consider a real-world example where we have an interface for a payment processing system. We want to log transactions and validate payment details using private methods.
Example:
public interface PaymentProcessor {
// Default method to process payment
default void processPayment(double amount) {
if (validateAmount(amount)) {
System.out.println("Processing payment of $" + amount);
logTransaction("Payment of $" + amount + " processed");
} else {
System.out.println("Invalid payment amount: $" + amount);
}
}
// Static method to refund payment
static void refundPayment(double amount) {
if (validateAmount(amount)) {
System.out.println("Refunding payment of $" + amount);
logTransactionStatic("Refund of $" + amount + " processed");
} else {
System.out.println("Invalid refund amount: $" + amount);
}
}
// Private method to validate amount
private boolean validateAmount(double amount) {
return amount > 0;
}
// Private method to log transaction
private void logTransaction(String message) {
System.out.println("Transaction Log: " + message);
}
// Private static method to validate amount
private static boolean validateAmountStatic(double amount) {
return amount > 0;
}
// Private static method to log transaction
private static void logTransactionStatic(String message) {
System.out.println("Transaction Log: " + message);
}
}
Explanation:
- processPayment(): A default method that processes a payment and uses the private methods
validateAmount()
andlogTransaction()
. - refundPayment(): A static method that refunds a payment and uses the private static methods
validateAmountStatic()
andlogTransactionStatic()
. - validateAmount(): A private method to validate the payment amount.
- logTransaction(): A private method to log the transaction.
- validateAmountStatic(): A private static method to validate the payment amount.
- logTransactionStatic(): A private static method to log the transaction.
5. Conclusion
Private methods in interfaces provide a way to encapsulate common logic and improve code maintainability by reducing duplication. They allow you to define helper methods that can be used by other default and static methods within the same interface. By using private methods, you can write cleaner and more maintainable code in your Java interfaces.