OOP의 4대 특성 중 하나로, 복잡한 시스템에서 핵심적인 부분만 표현하고 불필요한 세부사항을 숨기는 개념.
추상 클래스(Abstract Class)
abstract class Animal {
// 일반 메서드
public void eat() {
System.out.println("This animal eats food.");
}
// 추상 메서드
public abstract void makeSound();
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.eat(); // "This animal eats food."
dog.makeSound(); // "Bark"
}
}
인터페이스(Interface)
interface Vehicle {
void move();
}
class Car implements Vehicle {
@Override
public void move() {
System.out.println("Car is moving");
}
}
class Bicycle implements Vehicle {
@Override
public void move() {
System.out.println("Bicycle is moving");
}
}