Using Exceptions with Abstract Classes

A common question regarding abstract classes is how to use exceptions with their method declarations. What exceptions can an inheriting method throw with respect to the inherited method? Consider the following class, whose single method throws one exception: IOException.

public abstract class AbstractPrinter {

abstract void printMessage(String msg) throws IOException;
}

The implementation of this abstract class is shown in PrinterImp1.java. To implement an abstract class, you simply extend it, just as you would extend any other class, using the extends keyword.

PrinterImpl.java

 package net.javagarage.demo.abstractclasses; import java.io.IOException; public class PrinterImpl extends AbstractPrinter { void ...

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.