class A{
public String x(){return "A.x";}
}
class B extends A{
public String x(){return "B.x";} ⬅️ 얘가 수행됨
public String y(){return"y";}
}
public class PolymorphismDemo1 {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x());
}
}
- A obj = new B();
- System.out.println(obj.x());
- B.x
- 클래스B가 상위메소드를 오버라이딩 했다면 오버라이딩 한 것이 실행
* 오버라이딩: 자식이 재정의
생활코딩-다형성(3/6)
class A{
public String x(){return "A.x";}
}
class B extends A{
public String x(){return "B.x";}
public String y(){return "y";}
}
class B2 extends A{
public String x(){return "B2.x";}
}
public class PolymorphismDemo1 {
public static void main(String[] args) {
A obj = new B();
A obj2 = new B2();
System.out.println(obj.x());
System.out.println(obj2.x());
}
}
- System.out.println(obj.x());
- System.out.println(obj2.x());