
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it mandatory to override the default methods of an interface in Java?
The default methods are introduced in an interface since Java8. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.
In short, you can access the default methods of an interface using the objects of the implementing classes.
Example
interface MyInterface{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface"); } } public class InterfaceExample implements MyInterface{ public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.display(); } }
Output
display method of MyInterface
But, when your class implements two interfaces, and if they both have methods with same name and prototype. You must override this method else a compile time error is generated.
Example
interface MyInterface1{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface1"); } } interface MyInterface2{ public static int num = 1000; public default void display() { System.out.println("display method of MyInterface2"); } } public class InterfaceExample implements MyInterface1, MyInterface2{ public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); // obj.display(); } }
Compile time error
InterfaceExample.java:14: error: class InterfaceExample inherits unrelated defaults for display() from types MyInterface1 and MyInterface2 public class InterfaceExample implements MyInterface1, MyInterface2{ ^ 1 error
To resolve this you need to override either (or, both) of the display() methods in the implementing class −
Example
interface MyInterface1{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface1"); } } interface MyInterface2{ public static int num = 1000; public default void display() { System.out.println("display method of MyInterface2"); } } public class InterfaceExample implements MyInterface1, MyInterface2{ public void display() { MyInterface1.super.display(); //or, MyInterface2.super.display(); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.display(); } }
Output
display method of MyInterface1 display method of MyInterface2
- Related Questions & Answers
- Can we override default methods in Java?
- How to override only few methods of interface in Java?
- Is it mandatory to mark a functional interface with @FunctionalInterface annotation in Java?
- What happens if we overload default methods of an interface in java?
- What is the use of default methods in Java?
- Is it mandatory to use T for type-parameter, while defining Generics classes/methods in Java?
- Is it possible to override toString method in an array using Java?
- Can the abstract methods of an interface throw an exception in java?
- Java Interface methods
- Can we overload methods of an interface in Java?
- Is it mandatory to close JDBC connections?
- Default methods in Java
- Can we override private methods in Java
- Can we override final methods in Java?
- What are the advantages of private methods in an interface in Java 9?
Advertisements