public interface Animal {
void makeSound(); // 소리를 내는 메소드
}
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
Dog 클래스는 Animal 인터페이스를 구현(implements)하고, makeSound() 메소드를 정의해야 한다.
// 인터페이스 정의
public interface Vehicle {
void start(); // 시동을 걸다
void stop(); // 멈추다
}
// Car 클래스가 Vehicle 인터페이스 구현
public class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car is starting");
}
@Override
public void stop() {
System.out.println("Car is stopping");
}
}
// Bicycle 클래스도 Vehicle 인터페이스 구현
public class Bicycle implements Vehicle {
@Override
public void start() {
System.out.println("Bicycle is starting");
}
@Override
public void stop() {
System.out.println("Bicycle is stopping");
}
}
Vehicle 인터페이스는 start()와 stop() 메소드를 정의하지만, 구체적인 동작은 각각 Car와 Bicycle 클래스에서 다르게 구현된다.
Vehicle 인터페이스를 구현하는 모든 클래스는 반드시 start()와 stop() 메소드를 구현해야 하므로 일관성을 유지할 수 있다.Vehicle 인터페이스를 구현한 Car와 Bicycle 객체를 동일한 Vehicle 타입으로 처리할 수 있다./*
* 다형성 예시
*/
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle bike = new Bicycle();
car.start(); // Car is starting
bike.start(); // Bicycle is starting
}
}
[다중 인터페이스 구현 예시]
/*
* Flyable 인터페이스 정의
*/
public interface Flyable {
void fly(); // 비행 메소드
}
/*
* Swimable 인터페이스 정의
*/
public interface Swimable {
void swim(); // 수영 메소드
}
/*
* Duck 클래스가 Flyable과 Swimable 인터페이스를 구현
*/
public class Duck implements Flyable, Swimable {
@Override
public void fly() {
System.out.println("The duck is flying");
}
@Override
public void swim() {
System.out.println("The duck is swimming");
}
}
Vehicle 클래스(자동차, 자전거 등)를 만들고 싶을 때, Vehicle 인터페이스를 정의할 수 있습니다.인터페이스는 클래스가 따라야 할 설계도 역할을 한다. 하지만 인터페이스 자체에는 메소드의 구체적인 동작이 정의되어 있지 않고, 그저 메소드의 이름과 시그니처(입력과 출력)만 정의되어 있습니다. 이 인터페이스를 따르는 실제 클래스가 이 메소드들의 구체적인 동작을 구현하게 되는데, 그 구현한 클래스를 구현체라고 부릅니다.
/*
인터페이스 정의
*/
public interface Animal {
void makeSound(); // 메소드 시그니처만 정의 (구현 X)
}
/*
구현체 클래스
*/
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
public class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow");
}
}
Animal은 인터페이스로, makeSound()라는 메소드를 정의했지만, 구현되지 않음.Dog와 Cat 클래스는 구현체로, makeSound() 메소드의 구체적인 동작을 각각 다르게 구현Dog는 "Bark" 소리를 내고, Cat은 "Meow" 소리를 내는 방식으로 메소드를 구현Animal 인터페이스를 사용하는 코드는 Dog, Cat과 같은 다양한 구현체와 상호작용할 수 있다.Animal myAnimal = new Dog(); // Dog 객체를 참조
myAnimal.makeSound(); // "Bark" 출력
myAnimal = new Cat(); // Cat 객체를 참조
myAnimal.makeSound(); // "Meow" 출력
[인터페이스, 메소드, 그리고 객체 선언과 관련된 코드 예제]
/*
* Vehicle 인터페이스 정의
*/
public interface Vehicle {
// 메소드: 시동을 걸다
void start();
// 메소드: 멈추다
void stop();
}
/*
* Vehicle 인터페이스를 구현하는 Car 클래스
*/
public class Car implements Vehicle {
// 필드: 자동차의 색상
private String color;
// 생성자: Car 객체 생성 시 색상을 설정
public Car(String color) {
this.color = color;
}
// 메소드: 자동차가 시작할 때 호출됨
@Override
public void start() {
System.out.println(color + " Car is starting"); // 자동차 시작 메소드
}
// 메소드: 자동차가 멈출 때 호출됨
@Override
public void stop() {
System.out.println(color + " Car is stopping"); // 자동차 정지 메소드
}
}
/*
* Vehicle 인터페이스를 구현하는 Bicycle 클래스
*/
public class Bicycle implements Vehicle {
// 필드: 자전거의 종류
private String type;
// 생성자: Bicycle 객체 생성 시 종류를 설정
public Bicycle(String type) {
this.type = type;
}
// 메소드: 자전거가 시작할 때 호출됨
@Override
public void start() {
System.out.println(type + " Bicycle is starting"); // 자전거 시작 메소드
}
// 메소드: 자전거가 멈출 때 호출됨
@Override
public void stop() {
System.out.println(type + " Bicycle is stopping"); // 자전거 정지 메소드
}
}
/*
* 메인 클래스에서 Vehicle 인터페이스를 사용하는 예제
*/
public class Main {
public static void main(String[] args) {
// Vehicle 타입의 참조 변수 선언
Vehicle myCar = new Car("Red"); // Car 객체 생성 (색상: Red)
Vehicle myBicycle = new Bicycle("Mountain"); // Bicycle 객체 생성 (종류: Mountain)
// Car 객체의 메소드 호출
myCar.start(); // "Red Car is starting" 출력
myCar.stop(); // "Red Car is stopping" 출력
// Bicycle 객체의 메소드 호출
myBicycle.start(); // "Mountain Bicycle is starting" 출력
myBicycle.stop(); // "Mountain Bicycle is stopping" 출력
}
}
🤚
Vehicle타입의 참조 변수? Vehicle 인터페이스를 기반으로 한 변수
Vehicle 타입의 참조 변수는Vehicle 인터페이스를 구현한 클래스의 객체를 참조할 수 있는 변수를 의미한다. 자바에서 인터페이스는 객체가 따라야 하는 계약(규칙)을 정의하며, 이 인터페이스를 구현한 클래스의 객체를 참조할 수 있는 타입을 제공한다.
Vehicle myCar;와 같은 선언은 myCar라는 변수가 Vehicle 타입임을 나타낸다.Vehicle 인터페이스를 구현한 어떤 객체도 참조할 수 있다. 예를 들어, Car 또는 Bicycle 객체를 참조할 수 있다.장점으로는 다형성을 활용하여 여러 객체를 동일한 방식으로 다룰 수 있으며, 코드의 유연성과 일관성을 유지할 수 있다.