Private Methods in Interfaces – Object-Oriented Programming

Private Methods in Interfaces

Private methods in interfaces are no different from private methods in classes. As such, they can only be accessed inside the interface, acting as helper or auxiliary methods for non-abstract methods declared in the interface. They allow code to be shared between the non-abstract methods in the interface, thus promoting code reuse and avoiding code duplicity.

Private methods in an interface are governed by the following rules:

  • Not surprisingly, a private method must be declared with the private modifier in its header.
  • A private method can only be accessed inside the interface itself.
  • A private method cannot be declared with the abstract or the default modifier, but can be declared either as a private instance method or a private static method.
  • A private static method can be invoked in any non-abstract method in the interface.
  • A private instance method can only be invoked in default and private instance methods of the interface, and not in any static methods of the interface.

The above rules stem from accessibility of a private member in a type declaration in which the code is accessible in static and non-static contexts.

Example 5.15 illustrates declaring private methods in an interface. The default method getSalePrice() at (2) and the static method startSale() at (5) call the private instance method wrapUp() at (4) and the private static method showSaleItems() at (7), respectively.

Example 5.15 Private Methods in Interfaces

Click here to view code image

/** Interface with private methods. */
public interface IShopper {
  // Abstract method:
  double getItemPrice();                               // (1)
  // Default method:
  default double getSalePrice(double price) {          // (2)
    var salePrice = (80.0/100.0)*price;
    System.out.println(“Default method: ” + “Sale price is ” + salePrice);
    wrapUp();                    // (3) Calls the private instance method at (4)
    return salePrice;
  }
  // Private instance method:
  private void wrapUp() {                               // 4)
    System.out.println(“Private method: ” + “Wrapping up!”);
  }
  // Static method:
  static void startSale() {                             // (5)
    System.out.println(“Static method: ” + “Amazing savings!”);
    showSaleItems();             // (6) Calls the private static method at (7)
  }
  // Private static method:
  private static void showSaleItems() {                 // (7)
    System.out.println(“Private static method: ” + “Sorry. No items on sale!”);
  }
}

/** Class Shopper implements IShopper interface */
public class Shopper implements IShopper {
  @Override
  public double getItemPrice() {
    return 100.00;
  }
  public static void main(String[] args) {
    Shopper customer = new Shopper();
    var price = customer.getItemPrice();
    System.out.println(“Item price: ” + price);
    var salePrice = customer.getSalePrice(price); // Calls default method at (2)
    System.out.println();
    IShopper.startSale();                         // Calls static method at (5)
  }
}

Output from the program:

Click here to view code image

Item price: 100.0
Default method: Sale price is 80.0
Private method: Wrapping up!
Static method: Amazing savings!
Private static method: Sorry. No items on sale!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *