Dev_Study/Java_backup

형변환(Casting) Ex1

LeeDaniel 2016. 9. 14. 17:28
class Car{
  String color;
  int door;
	
  void drive(){
    System.out.println("부릉부릉!");
  }
	
  void stop(){
    System.out.println("정지합니다!");
  }
}//end of Car class

class FireEngine extends Car{
  void water(){
    System.out.println("물을 뿌립니다!");
  }
}//end of FireEngine class

public class CastingTest1 {
  public static void main(String[] args) {
    Car car = null;
    FireEngine fe = new FireEngine();
    FireEngine fe2 = null;
		
    fe.water();
    car = fe;
    car.water(); //Car클래스의 멤버변수가 아닌것은 사용할 수 없다
    fe2 = (FireEngine)car;
    fe2.water();
    fe2.drive();
    fe2.stop();
  }//end of main method
}//end of CastingTest1 class



반응형