Java 8 – Default Methods in Interface

Java 8 arrived with new features and advances which has also interface with default methods in its arsenal too.

What we might have heard about default methods in interfaces?

  • Multiple inheritance.
  • Backward Compatibility.
  • Default methods reduces the need of abstract class.

While the first two points are correct but the last point is not though it seems to more or less works like the abstract class. I will keep it for last, and starts with some basic information.

Why we needed Default Methods inside Interface?

As it is said, when there are problems we look around for solutions , this is the case where it get fits. Think of a situation where one needs to declare a new method signature inside an interface, and that interface has been implemented in 100’s of classes.  Hhhuh!! That is a painful task as first it is mandatory to  implement it all the the new method in 100 instances and also we need to handle the situation where we don’t need that function.

What if we are allowed to write a default method definition inside interface?
Listen behind, someone whispering “Java 8 allows you to do so…

Continue reading