
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
Can Abstract Methods of an Interface Throw an Exception in Java
Yes, the abstract methods of an interface can throw an exception.
Example
In the following example the interface (MyInterface) contains an abstract method with name display, which throws an IOException.
import java.io.IOException; abstract interface MyInterface { public abstract void display()throws IOException ; }
Rules to be followed
You need to follow the rules given below while implementing such method −
If the abstract method in the interface throws certain exception. The implemented method can throw the same exception as shown below −
Example 1
import java.io.IOException; abstract interface MyInterface { public abstract void display()throws IOException ; } public class InterfaceExample implements MyInterface{ public void display()throws IOException { System.out.println("This is the subclass implementation of the display method"); } public static void main (String args[]){ try { new InterfaceExample().display(); } catch (Exception e) { e.printStackTrace(); } } }
Output
This is the subclass implementation of the display method
If the abstract method in the interface throws certain exception. The implemented method can choose not to throw any exception as shown below −
Example 2
import java.io.IOException; abstract interface MyInterface { public abstract void display()throws IOException ; } public class InterfaceExample implements MyInterface{ public void display() { System.out.println("This is the subclass implementation of the display method"); } public static void main (String args[]){ try { new InterfaceExample().display(); } catch (Exception e) { e.printStackTrace(); } } }
Output
This is the subclass implementation of the display method
If the abstract method in the interface throws certain exception. The implemented method can throw its subtype −
Example 3
import java.io.IOException; abstract interface MyInterface { public abstract void display()throws Exception ; } public class InterfaceExample implements MyInterface{ public void display()throws IOException { System.out.println("This is the subclass implementation of the display method"); } public static void main (String args[]){ try { new InterfaceExample().display(); } catch (Exception e) { e.printStackTrace(); } } }
Output
This is the subclass implementation of the display method
If the abstract method in the interface throws certain exception. The implemented method should not throw its super type
Example 4
import java.io.IOException; abstract interface MyInterface { public abstract void display()throws IOException ; } public class InterfaceExample implements MyInterface{ public void display()throws Exception { System.out.println("This is the subclass implementation of the display method"); } public static void main (String args[]){ try { new InterfaceExample().display(); } catch (Exception e) { e.printStackTrace(); } } }
Compile time error
Output
InterfaceExample.java:8: error: display() in InterfaceExample cannot implement display() in MyInterface public void display()throws Exception { ^ overridden method does not throw Exception 1 error