public interface 인터페이스명 {
상수
추상메소드
default 메소드
private 메소드
static 메소드
}
// 기본 public abstrct class A { public abstract void a(); public abstract void b(); public void c() { }; } public class C extends A { @Override public void a() { } @Override public void b() { } } A obj = new B(); obj.a(); obj.b(); obj.c();
// 인터페이스 public interface A { public void a(); public void b(); default void c() {} } public class B implements A { @Override public void a(){} @Override public void b(){} } A obj = new B(); obj.a(); obj.b(); obj.c();
// ex) public interface Shape { public double getArea(); } public class Circle implements Shape { @Override public double getArea() {} } Shape obj = new Circle(); obj.getArea();
Shape 클래스
public interface Shape { // final 상수 public final static double PI = 3.141592; // static 생략 가능 // 추상 메소드 public double getArea(); // public, abstract 생략 가능 // default 메소드 ( 본문이 있는 메소드) public default void message() { System.out.println("나는 도형이다."); } }
Circle 클래스
public class Circle implements Shape{ private double radius; public Circle(double radius) { super(); this.radius = radius; } @Override public double getArea() { return PI * Math.pow(radius, 2); // PI는 intetface final에 선언해둔 값 } }
Main 클래스
public class Main { public static void main(String[] args) { Shape s = new Circle(1); System.out.println(s.getArea()); } }
결과
** 부모가 여럿 있을 경우 extends, implements 섞어 사용
public abstract class Phone { public abstract void call(); public abstract void sms(); }
Computer클래스
public interface Computer { public void game(); public void internet(); }
SmartPhone 클래스
// 상속(extends) 먼저, 구현(implements) 나중 public class SmartPhone extends Phone implements Computer { @Override public void call() { System.out.println("전화기능"); } @Override public void sms() { System.out.println("SMS기능"); } @Override public void game() { System.out.println("게임기능"); } @Override public void internet() { System.out.println("인터넷기능"); } }
Main 클래스
public class Main { public static void main(String[] args) { // 메소드 호출 연습 Phone p1 = new SmartPhone(); p1.call(); p1.sms(); ((Computer)p1).game(); // Computer로 캐스팅 해야 함 ((Computer)p1).internet(); Computer p2 = new SmartPhone(); ((Phone)p2).call(); // Phone 으로 캐스팅 해야 함 ((Phone)p2).sms(); p2.game(); p2.internet(); } }
결과
** implements 연속 사용 가능
public class Pet { private String petName; public Pet(String petName) { super(); this.petName = petName; } public String getPetName() { return petName; } public void setPetName(String petName) { this.petName = petName; } }
Dog 클래스
// extends Pet : 애완동물이다. // implements Walkable : 산책이 된다. public class Dog extends Pet implements Walkable{ public Dog(String petName) { super(petName); }
Cat 클래스
//extends Pet : 애완동물이다. //implements Walkable : 산책이 된다. public class Cat extends Pet implements Walkable{ public Cat(String petName) { super(petName); } }
Snake 클래스
//extends Pet : 애완동물이다. public class Snake extends Pet{ public Snake(String petName) { super(petName); } }
Person 클래스
public class Person { public void foodFeed (Pet pet, String food) { System.out.println(pet.getPetName() + "에게" + food + "주기"); } public void walk(Walkable pet) { System.out.println(((Pet)pet).getPetName() + "와 산책"); } }
Walkable 클래스
// 산책이 가능한 애완물(Pet)은 Walkable 인터페이스를 구현시키고, // 산책 메소드에서는 Walkable 타입의 애완동물만 받는다. public interface Walkable { }
Main 클래스
public class Main { public static void main(String[] args) { Dog dog = new Dog("백구"); Cat cat = new Cat("냥냥이"); Snake snake = new Snake ("낼름이"); Person person = new Person(); person.foodFeed(dog, "개껌"); // 백구에게 개껌주기 person.foodFeed(cat, "츄르"); // 냥냥이에게 츄르주기 person.foodFeed(snake,"쥐"); // 낼름이에게 쥐주기 person.walk(dog); // 백구와 산책 person.walk(cat); // 냥냥이와 산책 // person.walk(snake); // 실행을 못하게 막고 싶다. } }
결과