본문 바로가기

JAVA/끄적끄적

다형성에 대한 의문점

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package webprj.a04_inheritage;
 
import java.util.ArrayList;
 
class Fruit {
    String name;
    
    Fruit(String name){
        this.name=name;
    }
    void info() {
        System.out.print("과일은 "+name+"입니다.");
    }
}//end of F
 
class Apple extends Fruit {
    String color;
    
    Apple(String name, String color){
        super(name);
        this.color=color;
    }
    void info() {
        super.info();
        System.out.println(" 색상은 "+color);
    }
    
}//end of A
 
class Banana extends Fruit {
    String from;
    Banana(String name, String from){
        super(name);
        this.from=from;
    }
    void info() {
        super.info();
        System.out.println(" 주요 산지는 "+from);
    }
}//end of B
 
class WaterMelon extends Fruit {
    int price;
    WaterMelon(String name, int price){
        super(name);
        this.price=price;
    }
    void info() {
        super.info();
        System.out.println(" 가격은 "+price+"원");
    }
}//end of W
 
public class A02_Polymorphism {
    public static void main(String[] args) {
 
        Fruit ap = new Apple("사과","빨강색");
        Fruit ba = new Banana("바나나","필리핀");
        Fruit wm = new WaterMelon("수박",15000);
 
        ap.info();
        ba.info();
        wm.info();
        
        Apple pa = new Apple("사과","빨강색");
        Banana ab = new Banana("바나나","필리핀");
        WaterMelon mw = new WaterMelon("수박",15000);
        
        pa.info();
        ab.info();
        mw.info();
 
//--------------------------------------------------------
 
        Fruit standard = new Fruit("ㅇㅋ");
        standard.info();
        Fruit ap = new Apple("사과","빨강색");
        ap.info();
        standard.info();
 
 
    }
}
 
cs

도대체 왜 메인메서드에서 외부클래스를 불러올 때, 생성자를 사용할 때

 

왜! 

Fruit ap = new Apple("사과","빨강색"); 이렇게 쓸까?

Apple pa = new Apple("사과","빨강색");  쓰임이 이거랑 다를바가 없는데..

 

"하나의 상위 객체가 상속받은 하위 객체의 형태로 변형되어 사용되는 것을 말한다." 고 배웠는데

뭔 쓸모지? 변할이유가 뭐지?

 

흠.. 상위객체의 디폴트 값이 변하나? 싶어서 standard.info();로 찍어봤더니 그것도 아니야.

뭐지?

 

그냥 이런 기능도 있다~~ 그런건가?

뭔가 차이가 있으니까 만들었을텐데 뭐지 ..

 

인터페이스와 추상메서드에선 다형성이 필수야. 왜냐면 추상메서드를 쓰면 해당클래스도 추상클래스가 되고,

추상클래스는 혼자서 참조변수 생성자 만드는걸 못하거든. 그래서 다형성형태로 만들어.

 

근데 얘는 왜?????

상속에서 추상메서드와 인터페이스로 넘어가기 위한 교두보 같은 건가?

흠..

'JAVA > 끄적끄적' 카테고리의 다른 글

JS 혼자 공부필요해  (0) 2019.09.05