0

Java 8 – Redeclare default methods

In Java, you can extend from another interface using the extends keyword. But Java 8 has default methods. What happens if we extend another interface that has the same default methods as our interface?

It gets re-declared in the "child" interface.

Re-declare with different Implementation

Consider these codes. We have two (2) interfaces and one of them extends the other. The ParentInterface has a default method that returns the String "Parent interface". The other interface, ChildInterface, extends from the ParentInterface.

Output

Re-declare as an abstract method

If we do not wish to use any default implementation, we can re-declare the default method as an abstract method. It is now up to the users of the interface to provide their own implementations of the abstract method.

Output

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert