추상클래스
정의
특징
- 인스턴스 생성 불가
- 추상 메서드를 포함하고 있는 클래스
- 조상 클래스로서 중요한 의미를 갖는다. 어느 정도 틀을 갖춘 상태에서 새로운 클래스를 작성할 때 도움이 된다.
- 추상 클래스를 통해서 여러 객체를 배열로 생성할 수도 있다.
추상메서드
정의
- 선언부만 작성하고 구현부는 작성하지 않은 미완성 메서드
특징
- 상속받는 클래스에 따라 구현부가 달라질 수 있기 때문에 사용한다.
- 자식 클래스에서 자신에 맞게 구현해주어야 한다.
- 자식 클래스는 오버라이딩으로 추상메서드를 모두 구현해야 하고 하나라도 구현하지 않으면 자식도 추상클래스로 지정해 주어야 한다.
abstract class machine{
abstract void turn_on();
abstract void turn_off();
}
class btn_machine extends machine{
boolean power = false;
void turn_on(){
if(power){ power = !power; }
};
void turn_off(){
if(!power){ power = !power; }
};
}
abstract class smart_machine extends machine{
int status = 0;
void turn_on(int i){ status = i; };
void turn_off(){status = 0;}
}
public class Main{
public static void main(String[] args) {
Unit arr [] = { new zerg(), new terran(), new protoss()};
}
}
abstract class Unit{
int x, y;
abstract void move(int x, int y);
abstract void suicide();
void stop(int current_x, int current_y){ x=current_x; y=current_y; }
}
class zerg extends Unit{
int hp = 50;
void move(int x, int y){ this.x = x; this.y = y; };
void suicide(){ this.hp = 0; };
}
class terran extends Unit{
int hp = 100;
void move(int x, int y){ this.x = x; this.y = y; };
void suicide(){ this.hp = 0; };
}
class protoss extends Unit{
int hp = 75;
void move(int x, int y){ this.x = x; this.y = y; };
void suicide(){ this.hp = 0; };
}