如果子类中的方法与父类中的方法同名,且参数类型,参数个数,参数位置与父类完全一致时。这个时候,就说子类的方法完全覆盖了父类中的方法。比如:
class Father{
void sing(int i){ //父类的方法
System.out.println("I can sing "+i+" songs!");
}
}
class Son extends Father{
void sing(int i){ //子类重写覆盖了父类的方法
System.out.println("I can sing "+2*i+" songs!");
}
}
public class override extends Father{
public static void main(String[] args){
Father f = new Father();
f.sing(5); //调用父类的方法
Son s = new Son();
s.sing(5); //调用子类重写后的方法
}
}