extract the common method by using abstract modifier(제어자) and creating "Unit" abstract class :
abstractclassUnit {int x,y;//whatever class which inherit Unit should implement move methodabstractvoidmove(int x,int y);voidstop() {..}}classMarineextendsUnit {voidmove(int x,int y) {..}voidstimPack() {..}}classTankextendsUnit {voidmove(int x,int y) {..}voidchangeMode() {..}}classDropshipextendsUnit {voidmove(int x,int y) {..}voidload() {..}voidunload() {..}}
Unit[] group =newUnit[3];//Object[] group = new Object[3];//Object 배열을 생성할 순 있지만 여기에는 move라는 메서드가 없기 때문에//Object배열의 group[i]에 move가 있어도 Object에는 move가 없기 때문에 에러가 난다!group[0] =newMarine();group[1] =newTank();group[2] =newDropship();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
Easy to maintain
Easy to create class
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 =newGregorianCalendar();//specific//abstract what to returnCalendar 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();publicstaticCalendargetInstance(Locale aLocale) {returncreateCalendar(TimeZone.getDefault(), aLocale);}privatestaticCalendarcreateCalendar(TimeZone zone,Locale aLocale) {//...if(caltype !=null) {switch (caltype){//Child Classes of Calendar Class//BuddhistCalendar, JapaneseImperialCalendar, GregorianCalendarcase"buddhist"; cal =newBuddhistCalendar(zone,aLocale);break;case"japanese"; cal =newJapaneseImperialCalendar(zone,aLocale);break;case"gregory"; cal =newGregorianCalendar(zone,aLocale);break; }}