default and static methods in java are actually interface evolution in java 8 how?

aditya chaudhari
JavaDeveloperDiary — JDD
3 min readOct 9, 2021
interface in java8, default and static methods

Traditionally Interfaces in java are actually contract. A contract which says a class implementing interface must implement abstract method in the interface.
Interfaces are actually real drivers behind abstraction and polymorphism in java.
But what if we have one interface which is implemented in 100 classes and now we want to introduce new method in the interface?

Lets discuss below questions in this post.

Q. Why we need default and static methods in java ?
Q. What is default and static method and how to use them ? create them ?
Q. What is difference between Abstract class and interfaces ? specially after introduction to default and static method in interface?
Q. Is multiple inheritance possible in java from java 8 ? understand facts.

Its possible from java 8 to provide method implementation code inside interface itself , yes its possible using default and static methods in java8

Q. Why we need default and static methods in java ?
The real benefit for default method in interface is for API and libraries developer. If any new method is introduced in the interface if we use default method its not needed to change all implemented classes as all classes will inherit them automatically.

Have you ever created utility static classes which holds utility methods ? Remember Collections class which actually has many static method which are actually utility methods for Collection interface.
Isn’t it good if you able to create utility methods in the interface itself It ‘ll save creating additional class. Using static methods in interfaces can be compared to write static utility methods for a class in separate class.

Q. What is default and static method and how to use them ? create them ?
There are multiple places in JDK from java 8 where default method and static methods are used. Please refer one example below :

default and static methods java 8 interface : ref java 8 in action.

To create default method we just need to use default modifier while writing methods in interface same goes with static methods just using static keyword does the job.

Q. What is difference between Abstract class and interfaces ? specially after introduction to default and static method in interface?
You got it.
Traditionally in OOP java interfaces are like contract and abstract classes are like template/base classes for other concrete classes.
One of the intend behind abstract class in java was ability to provide method implementations which later can be used in subclasses.
But with introduction of default methods in java we can achieve same in interfaces as well.
There are two main difference between Abstract Class and Interface in java 8.
1. Abstract classes can have instance variables however interface cannot
2. Class can extend only from one abstract class, but a class can implement multiple interfaces.

Q. Is multiple inheritance possible in java ?
behavior(method) multiple inheritance is possible but not class multiple inheritance.
As we can implement multiple interfaces for a class and provide default implementation its possible to reuse any method implementation in the implemented class. how ? check below diagram and code.

single vs multiple inheritance : reference java 8 in action.

In the code below you can observe we can use default method from either interface A or B

Considering new capabilities added to interfaces by introducing default and static methods , interfaces are truly evolved and provided more powerful ways to write more elegant and robust software solutions.

Thanks for reading.
If you have any suggestions regarding topics or this post please let me know in comments . An applause would definitely encourage me to write more such content.

All java 8 related articles on this publication can be found here. Thanks.

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT