Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
911 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...