- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 the 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
- Related Articles
- Can a constructor throw an exception in Java?
- Can we define an abstract class with no abstract methods in Java?
- Can we overload methods of an interface in Java?
- While overriding can the subclass choose not to throw an exception in java?
- Can we throw an Unchecked Exception from a static block in java?
- What happens if a class does not implement all the abstract methods of an interface in java?
- Can we write an interface without any methods in java?
- Can we use private methods in an interface in Java 9?
- Can we synchronize abstract methods in Java?
- Can we create an object of an abstract class in Java?
- When to use an abstract class and when to use an interface in Java?
- \nHow to throw an exception from a static block in Java? \n
- Declare static variables and methods in an abstract class in Java
- While chaining, can we throw unchecked exception from a checked exception in java?
- How do you throw an Exception without breaking a for loop in java?
