Classes/Objects  «Prev  Next»


Lesson 7Interface declarations
Objective Describe the declaration and use of interfaces.

Java Interface Declarations

Java interfaces have evolved over the versions, and by Java 17, they have become even more versatile and powerful. Here's a detailed description of the declaration and use of interfaces in Java 17:

1. Declaration

To declare an interface in Java, you use the `interface` keyword:
public interface MyInterface {
    // ... contents of the interface
}

2. Abstract Method

Traditionally, interfaces were primarily used to declare abstract methods that implementing classes must override:
public interface Drawable {
    void draw();
}
Any class that implements this interface must provide an implementation for the `draw()` method:
public class Circle implements Drawable {
    @Override
    public void draw() {
        // ... implementation for drawing a circle
    }
}

3. Default Methods

Java 8 introduced `default` methods, allowing interfaces to provide a default implementation for methods. This is useful for adding new methods to interfaces without breaking existing implementations:
public interface Drawable {
    void draw();
    default void clear() {
        System.out.println("Default clearing");
    }
}
Implementing classes can choose to override the default method or use the provided implementation.

4. Static Methods

Interfaces can have static methods, introduced in Java 8. These methods belong to the interface itself and can't be overridden by implementing classes:
public interface UtilInterface {
    static int add(int a, int b) {
        return a + b;
    }
}
Usage: `int result = UtilInterface.add(5, 3);`

5. Private Methods

Since Java 9, interfaces can have private methods, which are used to break down default methods into smaller, reusable components:
public interface Drawable {
    void draw();
    
    default void enhancedDraw() {
        prepare();
        draw();
    }
    
    private void prepare() {
        // ... preparation logic
    }
}
Private methods are not visible to implementing classes or external entities.

6. Sealed Interfaces (Java 17 Feature)

Java 17 introduced sealed interfaces, allowing an interface to restrict which classes or interfaces can implement or extend it:
public sealed interface Shape permits Circle, Rectangle {
    // ... contents of the interface
}
In the above example, only `Circle` and `Rectangle` can implement the `Shape` interface.

7. Implementing Multiple Interfaces

A unique feature of Java interfaces is that a class can implement multiple interfaces, which offers a form of multiple inheritances:
public class Circle implements Drawable, Rotatable {
    // ... implementations of methods from both interfaces
}

8. Nested Interfaces** Interfaces can be nested within other interfaces or classes:
public interface OuterInterface {
    interface InnerInterface {
        // ... contents of the nested interface
    }
}
Java 17 further elevates the power and flexibility of interfaces, making them an indispensable feature in modern Java programming. It's essential for developers to be familiar with these capabilities to architect versatile, resilient, and forward-compatible software solutions in Java.


Properties of members of an interface

As of Java SE7, the only primitive type an interface can define is a constant. Once the constant is assigned, you cannot change the value of a constant. The variables of an interface are implicitly public, final, and static. For example, the following definition of the interface TestInterface,

interface TestInterface {
 int age = 11;
}

is equivalent to the following definition:
interface TestInterface {
 public static final int AGE = 11;
}
Interfaces are used to define one or more methods along with constants (see above) that are implemented by classes. However, they may also define
  1. inner classes, and
  2. inner interfaces.

See the following link for a detailed description of static nested classes and interfaces.
The following example ExecutorService is from the Oracle documentation.


public interface ExecutorService extends Executor {
 // Interface body
}


Valid modifiers for interfaces are public and abstract, that is the following interface declarations are equivalent.
interface A{}
public interface A{}
public abstract interface A{}

A public interface may be accessed outside of its package. All interfaces are implicitly abstract. The extends clause consists of extends followed by a comma-separated list of interfaces. An interface can only extend another interface.
For example, if an interface X extends interfaces Y and Z, then X inherits all of the constants and methods of the interfaces Y and Z.

interface Y{
 void draw();
}
interface Z{
 void move();
}
public interface X extends Y,Z{
}

The interface body consists of constant declarations, abstract method declarations, inner classes, and inner interfaces. Inner classes and interfaces are covered later in this module. A constant definition is the declaration and initialization of a variable that is public, static, and final. The variables modifiers are optional.
An abstract method declaration is specified as follows:
modifiers returnType methodName(arguments) throwsClause;

Note the semicolon replaces the method body. Methods are implicitly abstract and public. They may not be declared static, native, final, or synchronized.