부모클래스 같은 상위클래스라고 생각하면 된다
interface
상수와 추상메서드만 선언이 가능하다
상수(final 생략)...자동으로 final이라 인식
->오로지 상수만 가능하기 때문에
추상 메서드의 abstract 생략...추상메서드만 가능하기때문
public interface FoodGage {
//인터페이스는 상수와 추상메서드만 선언이 가능하다
String SHOPNAME="보슬보슬"; //상수(final 생략)...자동으로 final이라 인식
//->오로지 상수만 가능하기 때문에
//추상 메서드의 abstract 생략...추상메서드만 가능하기때문
public void order();
public void beadal();
}
자식클래스
class 가 class를 받을때 : extends
class 가 interface 받을때 : implements
interface 가 interface 받을때 : extends
->같은 종류끼리 받을때 extends
implements는 다중구현 가능하다
-> interface 여러개 있어도 자식클래스 하나에 다 상속 가능
ex) public class 하위클래스명 implements interface1 이름, interface2 이름
implements -> interface로 만든 추상메서드를 가져오기 위해서 사용
클래스 생성시 add해서 interface 파일 넣어서 자동 오버라이딩 가능
혹은 implements interface가 있는 클래스명 하면 기존 클래스명 아래 빨간줄이 뜨면서 자동 오버라이딩 가능
SHOPNAME="돈까스 집";
->오류발생 interface에서 자동으로 final로 만들었기 때문에 수정 불가능
//class 가 class를 받을때 : extends
//class 가 interface 받을때 : implements
//interface 가 interface 받을때 : extends
//->같은 종류끼리 받을때 extends
//implements는 다중구현 가능하다
//implements -> interface로 만든 추상메서드를 가져오기 위해서 사용
//클래스 생성시 add해서 interface 파일 넣어서 자동 오버라이딩 가능
//혹은 implements interface가 있는 클래스명 하면 기존 클래스명 아래 빨간줄이 뜨면서 자동 오버라이딩 가능
public class Food34 implements FoodGage {
@Override
public void order() {
System.out.println(SHOPNAME);
//SHOPNAME="돈까스 집"; //->오류발생 interface에서 자동으로 final로 만들었기 때문에 수정 불가능
System.out.println("음식을 주문합니다");
}
@Override
public void beadal() {
System.out.println(SHOPNAME);
System.out.println("음식을 배달합니다");
}
}
메인클래스
다형성처리
상위클래스 하위클래스에서 사용하기 때문에 똑같이 적용하면 됨
인터페이스명...부모클래스명과 동일하다고 보면됨
FoodGage f=null;
f=new Food34();
==Foodgage f=new Food34();
package day0629;
public class FoodBaedalMain {
public static void main(String[] args) {
//1
Food34 food=new Food34();
food.order();
food.beadal();
System.out.println();
//2
//인터페이스명...부모클래스명과 동일하다고 보면됨
FoodGage f=null;
f=new Food34();
//== Foodgage f=new Food34();
f.order();
f.beadal();
}
}