본문 바로가기

java

Step09_AbstractClass(abstract 사용 예시)

 

 

MainClass02, Weapon, Gun

 

Weapon
Gun
MainClass02
MainClass02 console

 

MainClass02.useWeapon(new Gun());

static으로 선언했기 때문에 객체생성 없이 useWeapon을 호출할 수 있다.

또 같은 클래스 안에 있기 때문에 MainClass02는 생략 가능하다. (예시에서는 생략하지 않았다.)

 

public static void useWeapon(Weapon w)

useWeapon이 인자로 weapon type을 요구한다. main 메소드에서 useWeapon 메소드를 실행할때 weapon type 인자가 아닌 Gun type 인자를 전달했음에도 정상 작동하는 이유는 Gun class에서 Weapon을 상속(extends)받기 때문이다.

 

console 창에 w.attack() 의 결과로 shot이 출력되는 이유는 Gun class에서 attack 메소드를 재정의(override) 했기 때문이다.