O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required