Parent reference type variable indicates child object. Likewise, interface as well.
Paremeter type is interface
: The parameter is ONLY implemented class!
Return type is interface
: Return type can be interface itself and the class that implements its interface.
Why return type is interface?
Because of Abstration, if the class implements interface, it can be return type. Here the Fighter implments Fightable(interface), that can be any other class if it implements Fightable(interface)
abstractclassUnit {int x,y;abstractvoidmove(int x,int y);voidstop() {System.out.println("멈춥니다");}interfaceFightable {publicabstractvoidmove(int x,int y);publicabstractvoidattack(Fightable f);//parameter type is interface!//f is implemented class!}classFighterextendsUnit imiplements Fightable {publicvoidmove(int x,int y) {System.out.println("["+x+","+y+"]로 이동합니다"); }publicvoidattack(Fightable f) {System.out.println(f+"를 공격합니다"); }//return type of method getFightable is interface Fightable.FightablegetFightable() {//return Fighter//because of interface polymorphism, it is possible.returnnewFighter();//Fighter = new Fighter();//return Fighter; }}//mainpublicclassFighterTest {publicstaticvoidmain(String[] args) {Fighter FFF =newFighter();Fightable f2 =FFF.getFightable();//match variable type = return type of method//return type of FFF.getFightable() is Fightable//so put it the Fightable type.System.out.println("Fighter");Fighter f =newFighter();f.move(100,200);//parameter type is interface//parameter Fighter implemented Fightablef.attack(newFighter());System.out.println("Unit");//Unit is parent class of . FighterUnit u =newFighter();u.move(100,200);u.stop();//error. bc Unit does not have attack method//u.attack(new Fighter());System.out.println("Fightable");//Unit is parent class of . FighterFightable f4 =newFighter();f4.move(100,200);f4.attack(f4); }}