자바 기초

오버라이딩

coding1842 2024. 1. 27. 20:52
package com.study.javastudy.study;

public class Car3 {
    public void run() {
        System.out.println("Ca3의 run 메소드");
    }
}

package com.study.javastudy.study;

public class Bus2 extends Car3{
    public void run() {
        super.run();
        System.out.println("Bus2의 run 메소드");
    }
}

package com.study.javastudy.study;

public class BusExam2 {
    public static void main(String[] args) {
        Bus2 bus = new Bus2();
        bus.run();
        // 부모로 부터 받은 메소드를 자식에서 재정의 하면 자식메소드가 나온다,
        // 그렇다고 부모 메서드가 사라지는 것은 아니다(run메서드를 동일하게 만들었을 때)
        // 그리고 자식에서 super로 호출하면 부모먼저 나오고 자식이 나온다
    }
}