Object Oriented Programming
// 추상화와 다형성을 활용한 예제
abstract class Animal {
// 추상화: 공통 행동 정의
abstract void makeSound();
void move() {
System.out.println("The animal moves.");
}
}
class Dog extends Animal {
// 다형성: 구체적인 동작 구현
@Override
void makeSound() {
System.out.println("Woof! Woof!");
}
}
class Cat extends Animal {
// 다형성: 구체적인 동작 구현
@Override
void makeSound() {
System.out.println("Meow!");
}
}
public class Main {
public static void main(String[] args) {
// 추상화를 활용한 다형성 사용
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound(); // Woof! Woof!
dog.move(); // The animal moves.
cat.makeSound(); // Meow!
cat.move(); // The animal moves.
}
}Last updated