Method OverridingMethod overriding occurs when a subclass implements a method that is already implemented in a superclass.
The method name must be the same, and the parameter and return types of the subclass's implementation must be subtypes of the superclass's implementation. You cannot allow less access than the access level of the superclass's method.
方法覆盖:一个子类实现一个在父类中已经存在的方法叫覆盖。
子类和父类方法名必须相同,子类中覆盖的方法的参数和返回值类型必须是父类中方法的参数的类型的子类型。子类覆盖方法的访问级别不能比父类中方法严格。
eg,
- class Timer {
-
public Date getDate(Country c) { ... }
- }
-
class USATimer {
-
public Date getDate(USA usa) { ... }
- }
Where Usa extends Country.Method OverloadingMethod overloading is when two methods share the same name but have a different number or type of parameters.
方法重载:当两个方法具有相同的方法名,但是有不同数目或不同类型的参数。
eg,
- public void print(String str) { ...
-
}
-
public void print(Date date) { ...
-
}
阅读(1167) | 评论(0) | 转发(0) |