📚추상화란 무엇일까?
추상화란 불필요한 정보를 제거하고 본질적인 특징만 남기는 것을 의미합니다.
💡인터페이스를 통해 추상계층을 표현해 봅시다.
public interface LifeForm {
void exist(); // ✅ 공통: 모든 생명체는 존재한다.
}
public interface Animal extends LifeForm {
void makeSound(); //✅ 공통: 모든 동물은 소리를 냅니다.
}
public class Cat implements Animal {
@Override
public void exist() {
System.out.println("고양이가 존재합니다.");
}
@Override
public void makeSound() {
System.out.println("야옹");
}
public void scratch() {
System.out.println("스크래치");
}
}
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.exist();
cat.makeSound();
cat.scratch();
}
}
public class LifeForm {
public void exist() {
System.out.println("존재합니다2"); // ✅ 공통: 모든 객체는 존재한다.
}
}
public class Animal extends LifeForm {
public void makeSound() {
System.out.println("소리를 냅니다2"); // ✅ 공통: 모든 생명체는 성장한다.
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("야옹2");
}
public void scratch() {
System.out.println("스크래치!");
}
}
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.exist();
cat.makeSound();
cat.scratch();
}
}