Java 9 Private Interface Methods

beginner
25 min

Java 9 Private Interface Methods

Welcome to our comprehensive guide on Java 9's exciting feature - Private Interface Methods! In this lesson, we'll dive deep into understanding what private methods in interfaces are, why they're useful, and how to use them in your projects.

Let's start by understanding the basics of interfaces and methods in Java.

Interfaces

An interface in Java is a collection of abstract methods. It defines a contract that a class implementing the interface must adhere to.

java
// Example of an interface public interface Shape { double PI = 3.14; // Static constants in interfaces void draw(); // Abstract method }

Methods in Interfaces

All methods in an interface are by default public abstract. This means they are public, don't have a body, and must be overridden in the implementing classes.

Private Methods in Interfaces (since Java 9)

With Java 9, we can now declare methods as private within an interface. These methods can only be accessed within the default methods or static methods of the same interface.

java
// Example of a private method in an interface public interface Shape { double PI = 3.14; // Default method default void draw() { // Call the private method printName(); // ... } // Static method static void printName() { System.out.println("Shape"); } // Private method private void validateDimensions() { // Validation logic for dimensions } }

In the above example, printName() is a private method within the Shape interface. It can only be accessed by the default method draw() or the static method printName().

Why Private Methods in Interfaces?

  1. Code Reusability: Private methods in interfaces can be reused across multiple default methods, improving code reusability.
  2. Encapsulation: Private methods in interfaces help in encapsulating complex logic within the interface, making the interface more modular and easier to manage.
  3. Readability: By grouping related methods (both public and private) within an interface, we can improve the readability and maintainability of our code.

Practical Usage

Let's create an interface for a Square and a Rectangle with a private method for calculating the area.

java
public interface Shape { double PI = 3.14; default double calculateArea() { // Call the private method calculateDimensions(); // ... } private double calculateDimensions() { // Calculate the area for the specific shape if (this instanceof Square) { double side = ((Square) this).getSide(); return side * side; } else if (this instanceof Rectangle) { Rectangle rectangle = (Rectangle) this; return rectangle.getLength() * rectangle.getWidth(); } throw new IllegalArgumentException("Unsupported shape"); } } // Implementing the Shape interface for Square and Rectangle public class Square implements Shape { private double side; public Square(double side) { this.side = side; } public double getSide() { return side; } } public class Rectangle implements Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return length; } public double getWidth() { return width; } }

In the above example, we have an AreaCalculator interface with a private method for calculating the area. We then create Square and Rectangle classes that implement the AreaCalculator interface and provide their own implementations for the calculateDimensions() method.

Quiz

Quick Quiz
Question 1 of 1

What is the primary purpose of declaring methods as private within an interface in Java 9?

We hope you enjoyed learning about private methods in interfaces in Java 9! In our next lesson, we'll dive deeper into default methods and explore some more practical examples.

Happy Coding! 🎯