1 GT; Overrinding方法应该与Parent类方法具有相同的参数列表.
4> overriden方法可以抛出相同或更窄的异常,而不是更广泛的异常.
*只是想知道为什么这么点*
* 2 – 为什么不是超类的子类?*
3 – 为什么访问级别应该限制较少?
4 – 为什么它应该抛出狭窄的异常?
根据我的理解它只是如果我创建一个父类refrence创建一个子类对象并尝试运行每个方案然后
让我们假设A是父类,B是子类,它们都有方法printAndReturnSomething()
public class A{
public B printAndReturnSomething(){
S.O.P("Inside A Print");
return new B();
}
}
现在我们将子类B作为
public class B extends A{
public A printAndReturnSomething(){ // I know this isn't possible to return A but had it been then
S.O.P("Inside A Print");
return new A();
}
}
现在,如果我做这样的事情
A a =new B();
现在因为我有一个A的引用,所以我希望返回类型是B类型
B returnedValue=a.printAndReturnSomething(); // But it actually calls the child class method and hence returns A. So here comes the contradiction.
Similaraly为场景3和4.我的理解是否正确?我错过了其他更相关的东西吗?