개발/JAVA
객체간 타입 형변환💡
코린이딱콩
2022. 8. 30. 01:58
✔️상속 관계에서 자식 클래스가 부모 클래스 타입으로 참조되는 것이 허용되며 이를 up-casting이라 합니다.
✔️한번 부모 클래스 타입의 클래스로 참조가 이루어진 이후 다시 자식 클래스로 참조하는 것을 down-casting 이라 합니다.
public class Shape {
public void draw() {
System.out.println("Drawing Shape");
}
}
public class Rectangle exteds Shape{
@Override
public void draw() {
System.out.println("Drawing Rectangle");
}
}
public class Ellipse extends Shape{
@Override
public void draw() {
System.out.println("Drawing Ellipse");
}
}
public class Line extends Shape{
@Override
public void draw() {
System.out.println("Drawing Line");
}
}
public class InheritanceAssist{
public static void main(String[] args) {
Shape shape = new Rectangle();//up-casting
Rectangle rect = (Rectangle) shape; //down-casting
}
}