Before Java 8, interfaces could have only abstract methods. The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface, then its implementation code has to be provided in the class implementing the same interface. To overcome this issue, Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface.
16 Default Method Executed
The default methods were introduced to provide backward compatibility so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods.
The interfaces can have static methods as well which is similar to static method of classes.
16 Static Method Executed
Default Methods and Multiple Inheritance
In case both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.
Default TestInterface1 Default TestInterface2
- Interfaces can have default methods with implementation in Java 8 on later.
- Interfaces can have static methods as well, similar to static methods in classes.
- Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.
This article is contributed by Akash Ojha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Static methods vs Instance methods in Java
- How does default virtual behavior differ in C++ and Java ?
- Default constructor in Java
- Default array values in Java
- Replace null values with default value in Java Map
- How to change the default icon of Android App
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.io.BufferedWriter class methods in Java
- java.lang.Character class methods | Set 1
- Private and final methods in Java
- Can we override private methods in Java?
- Java Interface methods
- ArrayList and LinkedList remove() methods in Java with Examples
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- BitSet class methods in Java with Examples | Set 3
- StringTokenizer methods in Java with Examples | Set 2
- Java.lang.Integer class and its methods
- java.util.Currency methods with example
- util.date class methods in Java with Examples