Can we override only one 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.

To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.

Example

 Live Demo

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 void demoMethod3() {
      System.out.println("This is demo method-3");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.demoMethod1();
      obj.demoMethod1();
      obj.demoMethod3();
   }
}

Output

This is demo method-1
This is demo method-2
This is demo method-3

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.

Example

 Live Demo

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();
   }
}

Output

InterfaceExample.java:6: error: InterfaceExample is not abstract and does not override abstract method demoMethod3() in Sample
public class InterfaceExample implements Sample{
       ^
1 error

But, still if you want to override only one abstract method −

You can leave remaining methods unimplemented as −

Example

 Live Demo

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() {
   }
   public void demoMethod3() {
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.demoMethod1();
      obj.demoMethod2();
      obj.demoMethod3();
   }
}

Output

This is demo method-1

Since int is not mandatory to implement default methods of an interface, you can declare remaining methods default as −

Example

 Live Demo

interface Sample {
   void demoMethod1();
   default void demoMethod2() {
      System.out.println("Default demo method 2");
   }
   default void demoMethod3() {
      System.out.println("Default demo method 3");
   }
}
public class InterfaceExample implements Sample {
   public void demoMethod1() {
      System.out.println("This is demo method-1");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.demoMethod1();
      obj.demoMethod2();
      obj.demoMethod3();
   }
}

Output

This is demo method-1
Default demo method 2
Default demo method 3

Updated on: 14-Oct-2019

814 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements