자바 기초

클래스 형변환

coding1842 2024. 1. 27. 21:03
package com.study.javastudy.study;

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

package com.study.javastudy.study;

public class Bus3 extends Car4{
    public void ppangppang() {
        System.out.println("빵빵");
    }
}

package com.study.javastudy.study;

public class BusExam3 {
    public static void main(String[] args) {
        Car4 c = new Bus3();
        c.run();

        Bus3 bus = (Bus3)c; // Bus3 타입으로 형변환을 시켜야 한다, 생성할 때 new Bus3()으로 생성해서 사용 가능
        bus.run();
        bus.ppangppang();
    }
}