Exception and Inheritance in Java

后端 未结 5 1995
旧巷少年郎
旧巷少年郎 2021-01-04 12:59

Suppose we have this problem

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

public class Child1 extends Father{
    public void method1() throws Exce         


        
5条回答
  •  盖世英雄少女心
    2021-01-04 13:12

    If super class method does not declare an Exception then subclass overridden method can not declare checked exception. So you can only use Unchecked exception.

    Other option is to allow Super class to declare ParentException and then child overridden methods can declare any exceptions which are child of ParentException

提交回复
热议问题