- 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 abstract method declaration include throws clause in java?
A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.
public abstract myMethod();
Yes, you can throw and exception from an abstract class.
Example
In the following Java program, we have a two classes: one abstract class with name MyClass which have an abstract method (display()) and, another class that extends the abstract class.
Here, the display() method abstract class throws an IOException.
Example
import java.io.IOException; abstract class MyClass { public abstract void display() throws IOException; } public class AbstractClassExample extends MyClass{ 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 AbstractClassExample().display(); } catch (IOException e) { e.printStackTrace(); } } }
Output
This is the subclass implementation of the display method
While overriding an abstract method from a subclass you need to follow these rules −
If the abstract method in the super-class throws certain exception. The implemented method can throw the same exception.
If the abstract method in the super-class throws certain exception. The implemented method can choose not to throw any exception.
If the abstract method in the super-class throws certain exception. The implemented method can throw its subtype
If the abstract method in the super-class throws certain exception. The implemented method should not throw its super type.