Software Development

Java 8 – Redeclare default methods

[wp_ad_camp_1]

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

[wp_ad_camp_2]

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.

[wp_ad_camp_3]

Output

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like