public class A {
public void f(A a) {
System.out.print("in A ");
}
}
public class B extends A {
public static void main(String[] args) {
B b = new B();
A a = new A();
b.f(b);
b.f(a);
}
}
Why by adding the following method in B class there will be a compilation error?
It's known that if method throws something, it doesn't have to be declared in the method header...
public void f(A a) {
System.out.println("in B");
throw new java.io.IOExeption();
}
Answer
It's known that if method throws something, it doesn't have to be declared in the method header
That's only true for unchecked exceptions. Your method throws an IOException
which is a checked exception so you must either catch it or declare that you throw it otherwise compilation will fail.
No comments:
Post a Comment