[상속-21] 인터페이스(Interface)

seratpfk·2022년 8월 3일
0

JAVA

목록 보기
73/96

인터페이스(Interface)

  • 클래스가 구현해야 할 메소드를 선언해 둔 자바 파일
  • 작업지시서 역할을 수행
  • 인터페이스 구현은 implements 키워드를 이용
  • 인터페이스를 구현하는 클래스는 반드시 인터페이스의 모든 추상메소드를 오버라이드 해야 함
  • 인터페이스에 작성하는 추상메소드는 abstract 키워드를 생략할 수 있음

Shape 인터페이스

public interface Shape {
	// final 상수
	public final static double PI = 3.141592;
	// 추상 메소드
	public double getArea();
	// 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);
	}
}

Main 클래스

public class Main {
	public static void main(String[] args) {
		Shape s = new Circle(1);
		System.out.println(s.getArea());
	}
}

출력:
3.141592

* 클래스를 여러 개 상속받는 것은 불가능하다.
* 하나를 인터페이스로 바꿔서 상속받을 수 있다.

Phone 클래스

public abstract class Phone {
	public abstract void call();
	public abstract void sms();
}

Computer 인터페이스

public interface Computer {
	public void game();
	public void internet();
}

SmartPhone 클래스

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("인터넷기능");
	}
}
  • SmartPhone 클래스는 Phone 클래스를 상속받고, Computer 인터페이스를 구현한다.
  • 상속(extends)가 먼저 나오고, 구현(implements)이 나중에 나온다.

Main 클래스

public class Main {
	public static void main(String[] args) {
		Phone p1 = new SmartPhone();
		p1.call();
		p1.sms();
		((Computer) p1).game();
		((Computer) p1).internet();
		Computer p2 = new SmartPhone();
		((Phone) p2).call();
		((Phone) p2).sms();
		p2.game();
		p2.internet();
		SmartPhone p3 = new SmartPhone();
		p3.call();
		p3.sms();
		p3.game();
		p3.internet();
	}
}

출력:
전화기능
SMS기능
게임기능
인터넷기능
전화기능
SMS기능
게임기능
인터넷기능
전화기능
SMS기능
게임기능
인터넷기능

Pet 클래스

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 클래스

public class Dog extends Pet implements Walkable {
	public Dog(String petName) {
		super(petName);
	}
}

Cat 클래스

public class Cat extends Pet implements Walkable {
	public Cat(String petName) {
		super(petName);
	}
}

Snake 클래스

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 인터페이스

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);    // 냥냥이와 산책
	}
}

출력:
백구에게 개껌주기
냥냥이에게 츄르주기
낼름이에게 쥐주기
백구와 산책
냥냥이와 산책

Song 클래스

public class Song {
	private String title;
	private double playTime;
	public Song(String title, double playTime) {
		super();
		this.title = title;
		this.playTime = playTime;
	}
	@Override
	public String toString() {
		return "Song [title=" + title + ", playTime=" + playTime + "]";
	}
}

Singer 클래스

public class Singer {
	private String name;
	private Song[] songs;
	private int idx;
	public Singer(String name, int cnt) {
		this.name = name;
		songs = new Song[cnt];
	}
	public void addSong(Song song) {
		if(idx == songs.length) {
			return;
		}
		songs[idx++] = song;
	}
	public void info() {
		System.out.println("가수이름 " + name);
		System.out.println("대표곡");
		for(int i = 0; i < idx; i++) {
			System.out.println(songs[i]);
		}
	}
}

Producer 클래스

public class Producer {
	public void produce(Singer singer, Song song) {
		singer.addSong(song);
	}
}

Main 클래스

public class Main {
	public static void main(String[] args) {
		Singer singer = new Singer("선미", 2);
		Song song1 = new Song("노래1", 3.5);
		Song song2 = new Song("노래2", 4.5);
        Producer producer = new Producer();
		producer.produce(singer, song1);
		producer.produce(singer, song2);
	}
}

출력:
가수이름 선미
대표곡
Song [title=노래1, playTime=3.5]
Song [title=노래2, playTime=4.5]

0개의 댓글