추상클래스

C/Java : 2007. 11. 6. 14:22

- 추상클래스는 미완성이다.

- 미완성 메서드를 포함하고 있다.

- abstract : 추상메서드가 있으니 상속을 통해 구현해야 한다는 의미

- 추상메서드 : 선언부로만 이루어진 메서드, 상속을 통해 자식클래스에서 구현부 정의.


 인터페이스


- 추상화의 정도가 높다.

- 추상메서드만을 구성으로 한다.

- 다른클래스를 작성하는데 도음을 주기위해 작성된다.


- 모든 멤버변수를 public static final로 선안하며 생략가능

- 모든 메서드는 public abstract로 선언하며 생략가능


- 클래스가 아닌 인터페이스로부터만 상속가능

- 여러개의 인터페이스로부터 다중상속 가능




public class FighterTest {
 public static void main(String[] args) {
  Fighter f = new Fighter();
  if(f instanceof Unit) {
   System.out.println("f는 Unit클래스의 자손입니다.");
  }
  if(f instanceof Fightable) {
   System.out.println("f는 Fightable인터페이스를 구현했습니다.");
  }
  if(f instanceof Movable) {
   System.out.println("f는 Movable인터페이스를 구현했습니다.");
  }
  if(f instanceof Attackable) {
   System.out.println("f는 Attackable인터페이스를 구현했습니다.");
  }
  if(f instanceof Object) {
   System.out.println("f는 Object클래스의 자손입니다.");
  }
 }
}


class Fighter extends Unit implements Fightable {
 public void move(int x, int y) { }
 public void attack(Unit u) { }
}


class Unit {
 int currentHP;
 int x;
 int y;
}


interface Fightable extends Movable, Attackable { }
interface Movable { void move(int x, int y); }
interface Attackable { void attack(Unit u); }

Posted by 청웨일