Java - Interface Abstract Methods Declarations

Introduction

The modifiers static and default are used to declare static and default methods, respectively.

The lack of static and default modifiers makes a method abstract.

The following is an example of an interface with all three types of methods:

interface AnInterface {
        // An abstract method
        int m1();

        // A static method
        static int m2() {
                // The method implementation goes here
        }

        // A default method
        default int m3() {
                // The method implementation goes here
        }
}

Abstract Methods Declarations

The following code declares an interface named Player:

public interface Player {
        public abstract void play();
        public abstract void stop();
        public abstract void forward();
        public abstract void rewind();
}

The above declaration of the Player interface can be rewritten as follows without changing its meaning:

public interface Player {
        void play();
        void stop();
        void forward();
        void rewind();
}

Abstract method declarations in an interface may include parameters, a return type, and a throws clause.

public interface Bank {
        boolean login(int account) throws AccountNotFoundException;
        boolean deposit(double amount);
        boolean withdraw(double amount) throws InsufficientBalanceException;
        double getBalance();
}

An abstract method in an interface cannot be declared final.