
결론 : <추상메서드의 집합 . 구현된 것이 하나도 없는 껍데기>
<다 public>
상수 추상메서드 default 메서드 정적 메서드가 있으나
추상메서드만 알면 된다! 이게 본질이다.
상수 추상메서드 디폴트 메소드 정적 메소드public입니다!!!static final 성질을 갖고 있다. (굳이 앞에 붙이지 않아도 그 성질을 갖고 있다)public interface PlayingCard {
public static final int SPADE = 4; //public static final 생략
final int DIAMOND = 3; //public static final 생략
static int HEART = 2; //public static final 생략
int CLOVER = 1; //public static final 생략
public abstract String getCardNumber(); //public abstract 생략
String getCardKind(); //public abstract 생략
}
그렇지만 이것▲들이 다 핵심은 아니다. 다 부수적인 것. 추상메서드만 이해하면 된다! by.자버지
<<추상 클래스: 일반 클래스인데 추상 클래스를 갖고 있다.
인터페이스 : 구현된게 아무것도 없는 것>>
다중 상속 가능(추상메서드는 충돌해도 문제 없음. 왜? 구현된게 없으니까 충돌할 게 없음)interface Fightable {
void move(int x, int y)
coid attack(Unit u);
}
abstract class Fighter implements Fightable
public void move(int x, int y) {...}
} >>추상 메서드 두개 중 하나만 구현한거니까 abstract 붙이기
class Fignter extends Unit implemnts Fightable{
public void move(int x, int y) { };
public void attack(Fightable f) { };
}
Unit u = new Fighter();
Fightable f = new Fighter();
▲클래스 상속과 마찬가지로, Fightable 인터페이스를 구현한 클래스의 인스턴스만 가능
Fightable method() {
Fightaer f = new Fighter();
return f; //인터페이스를 구현한 객체를 반환
}
Fightable f = new Fighter(); = Fightable f = method();
자식메소드니까.
abstract class Unit {
int x, y;
abstract void move(int x, int y);
void stop() {
System.out.println("멈춥니다");
}
}
interface Fightable {
void move(int x, int y);
void attack(Fightable f);
}
class Fighter extends Unit implements Fightable {
public void move(int x, int y) { //오버라이딩 규칙 : 조상(public)보다 접근제어자가 좁으면 안된다.
System.out.println("[" + x + "," + y + "]로이동");
}
public void attack(Fightable f) {
System.out.println(f + "를 공격");
}
}
public class FighterTest {
public static void main(String[] args) {
Fighter f = new Fighter();
f2.attack; ❌ Unit에는 attack이 없어서 호출 불가
f.move(100,200);
f.attack(new Fighter());
또는
Fighter f = new Fighter();
Fightable f2 = f.getFightable(); >>우회해서 interface 객체 만드네
}
}


class A {
public void method(B b) {
b.method();
}
}
class B {
public void method() {
System.out.println("B클래스의 메서드");
}
}
public class InterfaceTest {
public static void main (String[] args) {
A a= new A();
a.method(new B()); //A가 B를 사용(의존)
}
}
class A {
public void methodd(I i) { >>인터페이스 I를 구현한 넘들만 들어와라
i.method();
}
}
(부가적인거니까 그렇구나 하고 알고 계시면 돼요 by.자버지)