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.
Object-Oriented Programming
A paradigm that structures programs by bundling data and methods into objects. Encapsulation, inheritance, and polymorphism are its core concepts.
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.
# 객체지향 프로그래밍의 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
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free