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
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.
An overridden method must have