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

 Live Demo

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.

Updated on: 29-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements