方法重写:Rules for method overriding

2018-12-28  本文已影响1人  BenjaminCool

https://www.geeksforgeeks.org/overriding-in-java/

Overriding and Access-Modifiers :

Final methods can not be overridden :

Static methods can not be overridden(Method Overriding vs Method Hiding)

Private methods can not be overridden

The overriding method must have same return type (or subtype) :

Overriding and Exception-Handling

Rule#1 :

如果超类重写方法没有抛出异常,子类重写方法只能抛出 unchecked exception,
抛出 checked exception 将导致编译时错误。

Rule#2 :

If the super-class overridden method does throws an exception, subclass overriding method can only throw same, subclass exception.

Overriding and synchronized/strictfp method

对方法重写没有任何影响:
synchronized / strictfp方法可能会覆盖非synchronized / strictfp方法,反之亦然。

上一篇下一篇

猜你喜欢

热点阅读