상위 클래스로 부터 상속 받은 메서드와 동일한 이름의 메서드 재정의
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle {
void run() {
System.out.println("Bike is running");
}
}
class Car extends Vehicle {
void run() {
System.out.println("Car is running");
}
}
class MotorBike extends Vehicle {
void run() {
System.out.println("MotorBike is running");
}
}
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
Bike bike = new Bike();
Car car = new Car();
MotorBike motorbike = new MotorBike();
vehicle.run();
bike.run();
car.run();
motorbike.run();
}
Vehicle is running
Bike is running
Car is running
MotorBike is running
메서드 오버라이딩은 상위 클래스에 정의된 메서드를 하위클래스에 맞게 변경하고자 할 때 사용