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.