Java 8 – Default methods in interfaces

What is it?

  • Method in interface with default keyword.
  • Method marked default will have an implementation within interface itself.
  • Subclasses can override & provide custom implementation. If subclass does not provide, then default implementation from interface will be used.

When is it needed?

  • If interface was introduced long back & has many sub classes. Now you want to add new method but don’t want to impact existing sub classes.
  • If you don’t want to enforce all subclasses to implement method.

Output:

 

Leave a Reply

Your email address will not be published.