SIMPOLOR
단순하고 색있게
추상화(Abstraction)
단순색
·
2024년 06월 23일
Java
## 추상화(Abstraction)란? 추상화는 객체 지향 프로그래밍(OOP)의 중요한 개념으로, 복잡한 시스템에서 핵심적인 부분만을 모델링하여 단순화하는 것입니다. 주로 추상 클래스와 인터페이스를 통해 구현됩니다. ## 추상 클래스와 추상화 예제 ``` // 추상 클래스 정의 abstract class Animal { // 추상 메서드: 하위 클래스에서 반드시 구현해야 함 public abstract void makeSound(); // 일반 메서드 public void sleep() { System.out.println("Sleeping..."); } } // 추상 클래스 Animal을 상속받은 Dog 클래스 class Dog extends Animal { @Override // 추상 메서드 구현 public void makeSound() { System.out.println("Bark"); } } // 추상 클래스 Animal을 상속받은 Cat 클래스 class Cat extends Animal { @Override // 추상 메서드 구현 public void makeSound() { System.out.println("Meow"); } } public class Main { public static void main(String[] args) { // Animal 타입의 Dog 객체 생성 Animal myDog = new Dog(); myDog.makeSound(); // 출력: Bark myDog.sleep(); // 출력: Sleeping... // Animal 타입의 Cat 객체 생성 Animal myCat = new Cat(); myCat.makeSound(); // 출력: Mow myCat.sleep(); // 출력: Sleeping... } } ``` 1. **Animal 클래스**는 makeSound()라는 추상 메서드를 정의합니다. 이 메서드는 하위 클래스에서 반드시 구현해야 합니다. sleep() 메서드는 일반 메서드로, 모든 Animal 객체가 공통적으로 가질 수 있는 동작을 정의합니다. 2. **Dog와 Cat 클래스**는 Animal 클래스를 상속받아 makeSound() 메서드를 각각 Bark와 Meow로 구현하여 각 동물의 특성을 정의합니다. 3. 자식 클래스에서 부모 클래스의 메서드를 재정의하여, 자식 클래스에 맞게 동작을 변경할 수 있습니다. ## 인터페이스를 통한 추상화 예제 ``` // 인터페이스 정의 interface Shape { double calculateArea(); double calculatePerimeter(); } // 인터페이스 구현한 Circle 클래스 class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double calculateArea() { return Math.PI * radius * radius; } @Override public double calculatePerimeter() { return 2 * Math.PI * radius; } } // 인터페이스 구현한 Rectangle 클래스 class Rectangle implements Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } @Override public double calculateArea() { return length * width; } @Override public double calculatePerimeter() { return 2 * (length + width); } } public class Main { public static void main(String[] args) { // Shape 타입의 Circle 객체 생성 Shape myCircle = new Circle(5.0); System.out.println("Circle Area: " + myCircle.calculateArea()); System.out.println("Circle Perimeter: " + myCircle.calculatePerimeter()); // Shape 타입의 Rectangle 객체 생성 Shape myRectangle = new Rectangle(4.0, 6.0); System.out.println("Rectangle Area: " + myRectangle.calculateArea()); System.out.println("Rectangle Perimeter: " + myRectangle.calculatePerimeter()); } } ``` 1. **Shape 인터페이스**는 calculateArea()와 calculatePerimeter() 메서드를 정의하여 도형의 면적과 둘레를 계산하는 기능을 추상화합니다. 2. **Circle과 Rectangle 클래스**는 Shape 인터페이스를 구현하여 각 도형의 면적과 둘레를 계산하는 메서드를 제공합니다. 3. Shape 타입의 객체를 생성하더라도, 실제로는 Circle과 Rectangle 객체를 생성하여 사용함으로써 추상화된 Shape 타입의 이점을 활용할 수 있습니다. ## 마무리 추상화는 복잡한 시스템을 단순화하고, 핵심 기능만을 모델링하여 관리할 수 있게 해주는 중요한 개념입니다. 추상 클래스를 사용하면 기본 동작을 정의하고, 인터페이스를 통해 여러 클래스에서 공통적인 행동을 강제할 수 있습니다. 이러한 방식은 코드의 재사용성과 확장성을 높이고, 유지보수를 용이하게 합니다.
연관 포스트
BigDecimal 클래스
Java jar 실행 옵션
다형성(Polymorphism)
캡슐화(Encapsulation)
상속(Inheritance)