java-方法覆盖

class Car extends Vehicle {

public static void main(String[] args){
new Car().run();

}

 private final void run(){

System.out.println("car");
}

}
class Vehicle{
private final void run(){
System.out.println("vehicle");
}

}

分析:首先final声明的方法不能被覆盖,但是这里并不会错误,

因为父类的方法是private,就是说子类不可见(虽然继承,但是不可使用),所以子类没有覆盖,也就是说子类方法和父类是两个方法,此题调用的是子类的方法。

如果将父类的方法的private改成public,就会报错,因为final修饰的方法不能被覆盖

原文地址:https://www.cnblogs.com/itaylor/p/7783342.html