Exception and Inheritance in JAVA

Posted by user1759950 on Stack Overflow See other posts from Stack Overflow or by user1759950
Published on 2012-10-19T16:47:56Z Indexed on 2012/10/19 17:01 UTC
Read the original article Hit count: 148

Filed under:
|
|

Suppose we have this problem

public class Father{
    public void method1(){...}
}

public class Child1 extends Father{
    public void method1() throws Exception{
    super.method1();
    ... 
    }

}

Child1 extends Father and override method1 but given implementation Child1.method1 now throws a exception, this wont compile as override method can't throw new exceptions. What is the best solution?

  • Propagate the required exception to the Father.. to me this is against encapsulation, inheritance and general OOP ( the father potentially throw and exception that will never happen )
  • Use a RuntimeException instead? This solution wont propagate the Exception to the father but I read In Oracle docs and others sources states class of exceptions should be used when "Client code cannot do anything" this is not that case, this exception will b useful to recover blablabla ( why is wrong to use RuntimeException instead? )
  • Other..

thanks, Federico

© Stack Overflow or respective owner

Related posts about java

Related posts about exception