- 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 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.
- Related Articles
- Can we define an abstract class without abstract method in java?\n\n
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- Why can't static method be abstract in Java?
- Can we declare an abstract method final or static in java?
- Abstract Method and Classes in Java
- Class declaration with one method in Java
- If a method in parent class “throws Exception”, can we remove it in overridden method in java?
- Can we declare an abstract method, private, protected, public or default in java?
- Can we synchronize abstract methods in Java?
- Can we define an abstract class with no abstract methods in Java?
- Class declaration with a method that has a parameter in Java
- Can a class in Java be both final and abstract?
- Can constructors be marked final, abstract or static in Java?
- java variable declaration
- Throw and throws in Java
