learn/methodology

객체 지향 프로그래밍 (Object-Oriented Programming : OOP)

사겅이 2023. 9. 26. 05:19

출처 : https://veneas.tistory.com/entry/Java-%EA%B0%9D%EC%B2%B4-%EC%A7%80%ED%96%A5-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-Object-Orientied-Programming

 

소프트웨어 개발 패러다임 중 하나로, 현실 세계의 개념을 소프트웨어로 모델링하는 방법

 

출처 : https://www.apollotechnical.com/why-object-oriented-programming-matters/
출처 : https://coding-factory.tistory.com/864

 

  • 클래스(Class): 클래스는 객체를 만들기 위한 설계도로써 클래스는 속성(데이터)과 메서드(동작)를 포함
public class Car {
    // 속성(데이터)
    String brand;
    int year;

    // 메서드(동작)
    void start() {
        System.out.println("Car started!");
    }
}

 

  • 객체(Object): 클래스를 기반으로 실제로 생성된 것로써 클래스의 인스턴스임
public class Main {
    public static void main(String[] args) {
        // Car 클래스의 객체 생성
        Car myCar = new Car();

        // 객체의 속성 설정
        myCar.brand = "Toyota";
        myCar.year = 2023;

        // 객체의 메서드 호출
        myCar.start();
    }
}

 

  • 상속(Inheritance): 상속은 하위 클래스(subclass)가 상위 클래스(superclass)의 특성(속성과 메서드)을 상속받는 것
public class ElectricCar extends Car {
    // ElectricCar에만 해당하는 추가 속성과 메서드
    int batteryCapacity;

    void charge() {
        System.out.println("Charging the electric car.");
    }
}

 

  • 다형성(Polymorphism): 다형성은 객체가 여러 가지 형태로 동작할 수 있는 능력을 의미하며 메서드 오버라이딩 또는 인터페이스 구현을 통해 구현
public interface Animal {
    void makeSound();
}

public class Dog implements Animal {
    public void makeSound() {
        System.out.println("Dog barks.");
    }
}

public class Cat implements Animal {
    public void makeSound() {
        System.out.println("Cat meows.");
    }
}

 

  • 추상화(Abstraction): 추상화는 복잡한 시스템을 간소화하여 필수적인 부분만 표현하는 것을 의미하며 추상 클래스나 인터페이스를 사용하여 추상화를 구현
// 추상 클래스
abstract class Shape {
    abstract double calculateArea();
}

class Circle extends Shape {
    double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double calculateArea() {
        return Math.PI * radius * radius;
    }
}

 

  • 캡슐화(Encapsulation): 데이터와 데이터를 처리하는 메서드를 하나의 단위로 묶는 것
public class Student {
    private String name; // private로 선언된 데이터
    private int age;

    public void setName(String name) { // 데이터에 접근할 수 있는 메서드
        this.name = name;
    }

    public void setAge(int age) {
        if (age >= 0) { // 데이터 일관성 유지
            this.age = age;
        }
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

출처 : http://www.btechsmartclass.com/java/java-classes.html

'learn > methodology' 카테고리의 다른 글

테스트 주도 개발 (Test Driven Development : TDD)  (0) 2023.09.21
스크럼 (Scrum)  (0) 2023.09.21
애자일 (Agile)  (0) 2023.09.21
폭포수(Waterfall)  (0) 2023.09.21