Method Signature 标识 method name + method parameter list。
Definitions
Overriding means having two methods with the same method name and parameters (代表相同的method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.
Overloading occurs when two or more methods in one class have the same method name but different parameters(代表不同的method signature).
Overriding vs. Overloading
Overriding(动态多态)The real object type in the run-time, not the reference variable’s type, determines which overridden method is used at runtime. (overriding中,编译后两个方法的签名一样,所以需要通过程序运行时,由调用对象的对象类型决定调用哪个方法,而不是引用的变量类型。)
Overloading(静态多态)In contrast, reference type determines which overloaded method will be used at compile time.(overloading中,编译后多态方法具有不同的签名。)
An Example of Overriding
Here is an example of overriding. After reading the code, guess the output.
class Dog{
public void bark(){
System.out.println("woof ");
}
}
class Hound extends Dog{