코딩을 하다 콜렉션 객체를 사용하려고 


Map  map1 = new HashMap(); 으로 객체를 생성하려고 했는데 객체가 생성되는 것을 확인하였다.


나는 원래 대부분 객체를 생성할 때 

HashMap map1 = new HashMap(); 과 같이 생성자의 이름과 객체명 앞 타입을 앞뒤 동일하게 맞춰 생성하였다. 


HashMap map1 = new HashMap(); 

  Map map1 = new HashMap(); 이 뭐가 다른지 궁금했다.


Map map1 = new Map();은 되는 것인가? 


List list1 = new ArrayList(); 은 되는 것인가?


Collection map1 = new HashMap(); 은 되는가? 등등 


테스트를 통해 인터페이스와 클래스 간 호출, 클래스와 클래스 간의 호출에 대해 알 수 있었다.




작업 방법 및 소스 설명

1. Computer 클래스와 Laptop 클래스가 있다.


2. Computer 클래스에는 cpu, ram, power, mainboard 라는 속성이 있고


3. Laptop 클래스에는 Computer 클래스에는 없는 touchPad, wireless 속성이 있다.


4. Computer 클래스는 부모 클래스 이고 Laptop 클래스는 자식 클래스이다.


5. Laptop은 Computer 클래스를 상속받는다.


6. 객체를 생성할 때 Computer 클래스의 computer 변수명으로 Laptop 생성자를 호출시켜 객체 생성한다.


7. computer 객체가 Laptop 클래스의 메소드를 호출할 수 있는지 확인한다.


8. 부모클래스와 자식클래스 간의 호출이므로 computer 객체는 Laptop 클래스의 메소드를 호출 할 수 없으므로 

캐스팅을 이용한다.


9. 인터페이스에서 클래스 호출하는 것은 된다.


10. 인터페이스에서 인터페이스를 객체화 하는 것은 오류가 난다.


11. Map은 인터페이스 이므로 객체화 할수 없으므로 인터페이스 아래 클래스를 호출해 사용해야 한다.


12. Map은 인터페이스 이므로 메소드에 대한 선언만 있으므로 Map과 HashMap 두개의 선언 방식은 같다.




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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package test1;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class ObjectTest {
 
    public static void main(String[] args) {
        
 
        // 1. 부모클래스와 자식클래스 간의 호출
        
        // Computer 클래스로부터 computer이라는 변수명으로 Laptop 생성자 호출시키고 new 연산자 를 통해 객체를 저장할 메모리 할당 후 객체 초기화한다.
        Computer computer = new Laptop();
        computer.setCpu("i7-7700K");
        // ex) computer.setTouchPad("touchModelB"); -> 호출 안됨.
        // computer에선 Laptop 클래스의 메소드를 호출할 수 없다. 
 
        // computer 객체에서 Laptop 클래스의 메소드를 호출 하려면
        // 부모클래스에서 자식클래스 메소드 사용하기 위한 캐스팅이 필요하다.
        ((Laptop) computer).setTouchPad("touchModelB");
        System.out.println(((Laptop) computer).getTouchPad());        
        
        // 2. 인터페이스와 클래스 간의 호출
        
        Map map1 = new Map(); // -> 객체화 오류 발생
        
        // Map은 인터페이스 이므로 객체화 할수 없으므로 객체를 생성할 수 없다.
        // Map ConcurrentHashMap, HashMap, LinkedHashMap, TreeMap 같은 구현클래스를 선택해서 사용해야 한다.
        
        // 1. Map
        Map map2= new HashMap();
        map2.put(1"데이터1");
        System.out.println(map2.get(1));
        
        // 2. HashMap 
        HashMap hashmap1 = new HashMap();
        hashmap1.put(1"데이터1");
        System.out.println(hashmap1.get(1));
        
        // Map은 인터페이스 이므로 메소드에 대한 선언만 있으므로 두개의 선언 방식은 똑같다.
        
    }
}
 
// 컴퓨터 클래스에 속성 - cpu, ram, power, mainboard
class Computer{
    String cpu;
    String ram;    
    String power;
    String mainBoard;
    
    public Computer(){
    }
    
    public String getCpu() {
        return cpu;
    }
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }
    public String getRam() {
        return ram;
    }
    public void setRam(String ram) {
        this.ram = ram;
    }
    public String getPower() {
        return power;
    }
    public void setPower(String power) {
        this.power = power;
    }
    public String getMainBoard() {
        return mainBoard;
    }
    public void setMainBoard(String mainBoard) {
        this.mainBoard = mainBoard;
    }
}
 
// 랩탑의 속성 - 터치패드, 무선
class Laptop extends Computer{
    String touchPad;
    String wireless;
    
    public Laptop(){
        super();
    }
    
    public String getTouchPad() {
        return touchPad;
    }
    public void setTouchPad(String touchPad) {
        this.touchPad = touchPad;
    }
    public String getWireless() {
        return wireless;
    }
    public void setWireless(String wireless) {
        this.wireless = wireless;
    }
    
}
cs


해당 소스를 통해 Map은 메소드가 선언만 되어있는 인터페이스인 것을 확인했다.


마지막으로 해당 소스를 첨부한다.



ObjectTest.java




+ Recent posts