✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

Creating Abstract Class

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

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 :


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() {..}
}
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

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

Abstract <-> Specific

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

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

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 getInstance method

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;
    }
}
PreviousAbstract Class, Abstract MethodNextInterface

Last updated 4 years ago

Was this helpful?