Java重写和重载在编译时抛出错误?
我一直在编写一些关于方法重载和方法覆盖的示例。 方法重载是静态多态,重写是动态多态。
因此,与重载相关的任何错误都将在编译时被捕获。正确吗?或者是否存在由于不正确的重载而引发运行时异常的情况 - 我怀疑与
方法重写相关的任何错误( - 以防子类方法与基类具有相同的名称,并且与基类的参数相同----)将在编译时抛出。
上述两种情况是否存在运行时出现异常的情况?
I have been coding few examples on method overloading and method overridng.
Method overloading is static polymorphism and overriding is dynamic polymorphism.
So any error related to overloading will be caught at the compile time..Correct ?or are there any scenarios when a runtime exception is thrown because of incorrect overloading -- I Doubt
Any errors related to method overriding( -- incase when the child class method has same name as Base class and same arguments as of base class ---- )will be thrown at compile time .
Are there any scenarios in both the above cases where there is a exception at run time ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,如果方法中的代码不抛出任何运行时异常,则不应该重载。
这就是所谓的压倒一切。如果它不存在,那么如果您使用
@Override
,它可能会抛出编译时错误No it shouldn't if your code from the method doesn't throw any runtime exception.
This is what is called overriding. if its not there then it may throw compile time error if you use
@Override
我无法想象方法重载如何导致运行时异常。
不正确/不完整的方法重写的一些场景可能会导致运行时异常:
I cannot imagine how method overloading can cause a runtime exception.
Some scenarios when incorrect/incomplete method overriding can cause runtime exceptions: