💻 Programming

Object-Oriented Programming

Object-Oriented Programming

A paradigm that structures programs by bundling data and methods into objects. Encapsulation, inheritance, and polymorphism are its core concepts.

📖 Detailed Explanation

Object-Oriented Programming (OOP) is a paradigm that designs a program as an interaction of objects. Whereas procedural programming focuses on executing instructions sequentially, OOP modularizes by bundling data and the methods that process that data into a single unit (an object).

The four principles of OOP are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation hides the internal implementation and exposes only the interface. Abstraction represents a complex system as a simple model. Inheritance extends an existing class to reuse code. Polymorphism allows different implementations to be invoked through the same interface.

Most modern languages, such as Java, C++, Python, and C#, support OOP. A class is a blueprint for an object, and an object is an instance of a class. A class defines attributes (fields) and behaviors (methods), and an object is allocated in actual memory and operates.

OOP improves maintainability and extensibility in large-scale software development. However, excessive abstraction can increase complexity, so it is important to strike a balance by applying the SOLID principles and design patterns together.

💻 Code Example

Python
# 객체지향 프로그래밍의 4대 원칙 예제

# 추상 클래스 (추상화)
from abc import ABC, abstractmethod

class Animal(ABC):
    def __init__(self, name):
        self._name = name  # 캡슐화: protected 속성

    @property
    def name(self):  # getter로 접근 제어
        return self._name

    @abstractmethod
    def speak(self):  # 추상 메서드
        pass

# 상속과 다형성
class Dog(Animal):
    def speak(self):
        return f"{self.name}: 멍멍!"

class Cat(Animal):
    def speak(self):
        return f"{self.name}: 야옹~"

# 다형성 활용: 동일한 인터페이스, 다른 동작
def animal_sound(animal: Animal):
    print(animal.speak())

# 사용 예시
dog = Dog("바둑이")
cat = Cat("나비")

animal_sound(dog)  # "바둑이: 멍멍!"
animal_sound(cat)  # "나비: 야옹~"

# 캡슐화: 내부 상태 보호
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # private

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount

    def get_balance(self):
        return self.__balance

🗣️ Examples of Practical Conversations

Team lead
"We need to add a new payment method to the payment system. Is it possible without modifying the existing code?"
Developer
"You just need to add a new class that implements the PaymentProcessor interface. It's designed with the strategy pattern, so there's no need to touch the existing code."
Team lead
"The OCP (Open-Closed Principle) is well applied. For testing, we'll only need to add the new class, too."
Interviewer
"Please explain why composition should be preferred over inheritance."
Candidate
"Inheritance creates tight coupling, so changes to a parent class affect its children. Composition includes objects that have the needed functionality, so it is flexible and allows behavior to be changed at runtime."
Interviewer
"What is the Liskov Substitution Principle (LSP) among the SOLID principles?"
Candidate
"A child class must be able to replace its parent class without the program breaking. For example, if a square inherits from a rectangle, changing the width and height independently can violate the LSP."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer
"This class is doing everything from data validation to storage to sending notifications. It would be better to separate them according to the SRP (Single Responsibility Principle)."
Author
"Right. I'll split it into Validator, Repository, and NotificationService, and change this class so that it only coordinates."
Reviewer
"Also, if you depend on an interface instead of a concrete class, injecting a Mock for testing becomes easier too."

⚠️ Cautions

🔗 Related Terms

📚 Learn More