
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Override a Method While Implementing Java Interface
An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or make a contract specifying how the methods and fields of a type should be, you can define an interface. When a class implements an interface, it should provide concrete implementations for all of its abstract methods. But is it possible to override only one method from an interface?
Overriding a Method from an Interface
In Java, all methods in an interface are abstract (unless declared as default or static). When a class implements an interface, it must override those abstract methods.
No, you cannot override just one method from an interface if it has multiple abstract methods in a non-abstract class. Java will throw a compile-time error unless all abstract methods are implemented.
While implementing an interface, it is mandatory to override all the abstract methods of it; if you skip overriding any of the abstract methods, a compile-time error will be generated.
interface Sample { void demoMethod1(); void demoMethod2(); void demoMethod3(); } public class InterfaceExample implements Sample { public void demoMethod1() { System.out.println("This is demo method-1"); } public void demoMethod2() { System.out.println("This is demo method-2"); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.demoMethod1(); obj.demoMethod2(); } }
Let us compile and run the above program, this will give the following result -
InterfaceExample.java:6: error: InterfaceExample is not abstract and does not override abstract method demoMethod3() in Sample public class InterfaceExample implements Sample{ ^ 1 error
However, there are some cases where overriding one method is allowed, and they are -
-
Abstract Class Implementing an Interface
-
Using Default Methods
-
Using Static Methods
Abstract Class Implementing the Interface
If an abstract class implements an interface, it does not need to implement all methods. The concrete subclass must implement the remaining methods.
interface Animal { void eat(); void sleep(); void makeSound(); } abstract class WildAnimal implements Animal { public void eat() { System.out.println("Wild animal eats meat."); } // sleep() and makeSound() are not implemented here } public class Lion extends WildAnimal { public void sleep() { System.out.println("Lion sleeps in the den."); } public void makeSound() { System.out.println("Lion roars loudly."); } public static void main(String[] args) { Lion lion = new Lion(); lion.eat(); // From WildAnimal class lion.sleep(); // From Lion class lion.makeSound(); // From Lion class } }
Let us compile and run the above program, this will give the following result -
Wild animal eats meat. Lion sleeps in the den. Lion roars loudly.
Overriding Default Methods
Java 8 introduced default methods in interfaces. These methods have a default implementation in the interface itself. You can override only the methods that are abstract or default methods in the interface.
Since it is not mandatory to implement default methods of an interface, you can override only one method and declare the remaining as default -
interface Animal { void eat(); // Abstract method default void sleep() { System.out.println("Animal sleeps."); } default void makeSound() { System.out.println("Animal makes sound."); } } class Dog implements Animal { public void eat() { System.out.println("Dog eats bones."); } // No need to override sleep() or makeSound() } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Overridden method dog.sleep(); // Inherited default method dog.makeSound(); // Inherited default method } }
Let us compile and run the above program, this will give the following result -
Dog eats bones. Animal sleeps. Animal makes sound.
Using Static Methods
In Java 8, interfaces can have static methods. These methods belong to the interface itself, not to the implementing classes. You only implement the abstract (non-static) methods. So this is another way to implement only one method from an interface.
interface Animal { void eat(); // abstract method // static method in interface static void breathe() { System.out.println("Animal is breathing"); } } class Dog implements Animal { public void eat() { System.out.println("Dog is eating"); } } public class Test { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Calls overridden method // Calling static method from interface using interface name Animal.breathe(); // Static method called using interface name } }
Let us compile and run the above program, this will give the following result -
Dog is eating Animal is breathing