[JAVA/OOP]Abstract class & method

package class_abstract_common;

abstract class unit

{

static int x=10;

static int y=20;

abstract void move(int x, int y);

void stop()

{

System.out.println("stop");

};

}

class Tank extends unit

{

@Override

void move(int x, int y) {

// TODO Auto-generated method stub

x=super.x;

y=super.y;

System.out.println("Move to: ( "+x+", "+y+" )");

}

void changeMod()

{

System.out.println("Change the mode");

}

}

public class Marine extends unit{

public static void main(String[] args) {

// TODO Auto-generated method stub

Marine m=new Marine();

m.move(0, 1);

m.stimpack();

System.out.println();

Tank t=new Tank();

t.move(x, y);

t.changeMod();

}

@Override

void move(int x, int y) {

// TODO Auto-generated method stub

System.out.println("Move to: ( "+x+", "+y+" )");

}

void stimpack()

{

System.out.println("use stimpack!");

}

}

Characteristic of abstract

  1. Must override abstract class's method in child class
  2. Can define common method in one class(공통된 메서드를 한번만 뽑아내어 정리 후, 오버라이딩하여 사용 가능)

      Same with this!

      Unit[] group=new Unit[2];

      unit[0]=new A();

      unit[1]=new B();

      for(int i=0;i"<"group.length;i++)

      {

      unit[i].move();

      }

      Didn't CALL abstract class !!

      It calls its own method(override)!!

  3. Absence of '{}' is difference of instance/static method and abstact class
    • instance/static method
      void Method1(){}; static int Method2(){};
    • abstract method
      abstract Method1();