Java exception in method signature in subclasses

interface Printable {
    void print() throws IOException;
}

class Doc1 implements Printable {

    @Override
    public void print() throws IOException { // ok as the same exception
    }
}

class Doc2 implements Printable {
    
    @Override
    public void print() throws FileNotFoundException { // ok as FileNotFoundException extends IOException
    }
}

    // not possible as Exception is not a subclass of IOException (not to violate Liskov substitution principle)
    //public void print() throws Exception { // Exception is not compatible with throws clause in Printable.print()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s