반응형
Array of Objects (abstract) / 여러 객체의 배열 (추상)
조상 클래스를 추상화 하여 선언한다.
Abstract된 Class와 method는 미완성 되었다는 의미를 내포하고 있으며 Method overriding시 재정의 해야한다.
Abstract는 Inheritance를 통해서 Abstract Method를 완성시킨다.
abstract class Unit {
int x, y;
abstract void move(int x, int y);
void stop(){
}
}
각각의 Class를 Unit Class에 상속 받는다.
public class Marine extends Unit { //Unit에게 상속을 받는다.
void move(int x, int y){
System.out.println("Marine [x= " + x + ",y=" + y + "]");
}
public void stimPack(){
System.out.println("스팀팩 사용");
}
}
public class Tank extends Unit {
void move(int x, int y){
System.out.println("Tank[x= " + x + ",y=" + y + "]");
}
void changeMode(){
System.out.println("change Mode");
}
}
public class Dropship extends Unit {
void move(int x, int y){
System.out.println("Dropship[x= " + x + ",y=" + y + "]");
}
void load(){
System.out.println("탑승");
}
void unload(){
System.out.println("내림");
};
}
다형성의 성질로 조상 타입의 Referance Variable를 참조하는것이 가능하기 때문에 Object Array를 통해서 서로 다른 종류의 Instance를 하나의 묶음으로 다룰 수 있고아래의 코드처럼 조상 타입의 Array에 자손 타입의 Instance를 담을 수 있는 것이다.
public class main {
public static void main(String[] args) {
//객체 배열 선언
Unit[] group = {new Marine(), new Tank(), new Dropship()};
for(int i = 0; i < group.length; i++) {
group[i].move(100, 200);
}
}
}
반응형
'JAVA' 카테고리의 다른 글
[Java] Object Class, Wrapper Class, Arrays Class (0) | 2022.06.22 |
---|---|
[Java] Exception handling (예외처리) (0) | 2022.06.16 |
[Java] Polymorphism of Parameters (매개변수의 다형성) (2) | 2022.06.14 |
[Java] Interface (0) | 2022.06.09 |
[Java] Inheritance (상속) (0) | 2022.06.08 |