Search code examples
javaexceptionabstractthrow

Java abstract classes which throw


If I have an abstract class with the following function -

abstract class A{
    void foo(String s) throws Exception{
        throw new Exception("exception!");
    }
}

And then another class that extends the abstract class and implements its own version of foo -

class B extends A{
    void foo(String s){
        //do stuff that does *not* throw an exception
    }
}

Will this create problems? Specifically in the following test case -

Collection<A> col = new Collection<A>();
B b = new B();
col.add(b);
for(A a : col){
    a.foo();
}

I did some testing and nothing seems to have broken, but I don't understand why B's foo was called and not A's


Solution

  • Because of Polymorphism.

    Since, at runtime the actual object's type in the Collection is B so, B.foo() was called.

    Basically, if you have a sub-type object assigned to a super-class reference the runtime polymorphism makes sure that the sub-type's version of an instance method gets called i.e. if it has been overridden of course. If not, the call falls back upon the super-class version.

    What qualifies as a valid method override?

    An overridden method must have

    • the same method signature
    • a covariant return type (a sub-type can be returned)
    • must not throw broader checked exceptions (applies to your question and @Dgrin91's comment i.e. just because the overridden method took some risks (threw exceptions) doesn't mean that the overriding method should do the same; so, it may not throw any exceptions at all)
    • must not use a less restrictive access modifier (can make protected to public but not private)