> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/java/creating-abstract-class.md).

# Creating Abstract Class

-여러 클래스에 공통적으로 사용될 수 있는 추상클래스를 바로 작성하거나 기존 클래스의 공통 부분을 뽑아서 추상클래스를 만든다.

```java
class Marine {
    int x,y;
    void move(int x, int y) {..}
    void stop() {..}
    void stimPack() {..}
}
class Tank {
    int x,y;
    void move(int x, int y) {..}
    void stop() {..}
    void changeMode() {..}
}
class Dropship {
    int x,y;
    void move(int x, int y) {..}
    void stop() {..}
    void load() {..}
    void unload() {..}
}
```

extract the common method by using abstract modifier(제어자) and creating "Unit" abstract class :&#x20;

```java

abstract class Unit {
    int x,y;
    //whatever class which inherit Unit should implement move method
    abstract void move(int x, int y);
    void stop() {..}
}

class Marine extends Unit {
    void move(int x, int y) {..}
    void stimPack() {..}
}
class Tank extends Unit {
    void move(int x, int y) {..}
    void changeMode() {..}
}
class Dropship extends Unit {
    void move(int x, int y) {..}
    void load() {..}
    void unload() {..}
}
```

```java
Unit[] group = new Unit[3];
//Object[] group = new Object[3];
//Object 배열을 생성할 순 있지만 여기에는 move라는 메서드가 없기 때문에
//Object배열의 group[i]에 move가 있어도 Object에는 move가 없기 때문에 에러가 난다!
group[0] = new Marine();
group[1] = new Tank();
group[2] = new Dropship();

for(int i = 0; i < group.length; i++) {
    //Unit이 아니라 상속받은 각각의 객체에 접근, 실제로 구현된 메소드를 호출한다.
    group[i].move(100,200);
}
```

For someone who don't get it understand why use Abstract class

1. Easy to maintain
2. Easy to create class
3. Remove duplication

![](/files/-MIJtRVtO0Fxx5WkXlT9)

by using Meaningful steps of abstract class, can use mid class.

![](/files/-MIJuC7qjN4hNfKfM08T)

**Abstract <-> Specific**

Abstract code is more easier than specified code to modify code.

추상 클래스 타입 참조 변수로 자손 객체 담을 수 있다.

```java
GregorianCalendar cal = new GregorianCalendar();//specific
//abstract what to return
Calendar cal = Calendar.getInstance();//abstract. return child object.
```

**once write abstract code, it is more broad.**

**no need to create every single calendar.**

**easy to modify : just modify&#x20;*****getInstance*****&#x20;method**

```java
Calendar cal = Calendar.getInstance();

public static Calendar getInstance(Locale aLocale) {
    return createCalendar(TimeZone.getDefault(), aLocale);
}

private static Calendar createCalendar(TimeZone zone, Locale aLocale) {
    //...
    if(caltype != null) {
        switch (caltype){
        //Child Classes of Calendar Class
        //BuddhistCalendar, JapaneseImperialCalendar, GregorianCalendar
        case "buddhist";
            cal = new BuddhistCalendar(zone,aLocale);
            break;
        case "japanese";
            cal = new JapaneseImperialCalendar(zone,aLocale);
            break;
        case "gregory";
            cal = new GregorianCalendar(zone,aLocale);
            break;
    }
}
```
