Implementing interfaces in an abstract class

suggest change

A method defined in an interface is by default public abstract. When an abstract class implements an interface, any methods which are defined in the interface do not have to be implemented by the abstract class. This is because a class that is declared abstract can contain abstract method declarations. It is therefore the responsibility of the first concrete sub-class to implement any abstract methods inherited from any interfaces and/or the abstract class.

public interface NoiseMaker {
    void makeNoise();
}

public abstract class Animal implements NoiseMaker {
    //Does not need to declare or implement makeNoise()
    public abstract void eat();
}

//Because Dog is concrete, it must define both makeNoise() and eat()
public class Dog extends Animal {
    @Override
    public void makeNoise() {
        System.out.println("Borf borf");
    }

    @Override
    public void eat() {
        System.out.println("Dog eats some kibble.");
    }
}

From Java 8 onward it is possible for an interface to declare default implementations of methods which means the method won’t be abstract, therefore any concrete sub-classes will not be forced to implement the method but will inherit the default implementation unless overridden.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Interfaces:
* Implementing interfaces in an abstract class

Table Of Contents
8 Arrays
9 Interfaces
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap