Object-Oriented Programming (OOP) organizes code around objects — data structures that combine state (fields/properties) and behavior (methods). Objects are instances of classes that define their structure. OOP's four pillars: encapsulation, inheritance, polymorphism, and abstraction.

How OOP Works

A Dog class has properties (name, breed) and methods (bark, fetch). Each Dog object holds its own data. Encapsulation hides internal state — you call dog.feed() without knowing how hunger is tracked. Inheritance lets GoldenRetriever extend Dog, inheriting all behavior and adding its own.

Modern OOP favors composition over inheritance: instead of class GoldenRetriever extends Dog extends Animal, compose behaviors: a Dog has a Walker, a Swimmer, and a Fetcher. This is more flexible and avoids deep inheritance hierarchies.

Why Developers Use OOP

OOP is the foundation of Java, C#, Python, TypeScript, and most enterprise software. Understanding classes, interfaces, and SOLID principles is essential for working in any object-oriented codebase.

Key Concepts

  • Encapsulation — Bundle data and methods together, hide internal state — only expose what's needed
  • Inheritance — Create new classes based on existing ones — reuse and extend behavior
  • Polymorphism — Different objects respond to the same method call in different ways — enables flexible, extensible code
  • Abstraction — Define interfaces without implementation details — focus on what, not how
  • Composition over Inheritance — Build complex objects from simple components — more flexible than deep class hierarchies
  • SOLID Principles — Five design principles for maintainable OOP code — Single Responsibility, Open/Closed, etc.

OOP Example in TypeScript

typescript
interface Shape {
  area(): number;
  perimeter(): number;
}

class Circle implements Shape {
  constructor(private radius: number) {}

  area(): number {
    return Math.PI * this.radius ** 2;
  }

  perimeter(): number {
    return 2 * Math.PI * this.radius;
  }
}

class Rectangle implements Shape {
  constructor(private width: number, private height: number) {}

  area(): number {
    return this.width * this.height;
  }

  perimeter(): number {
    return 2 * (this.width + this.height);
  }
}

// Polymorphism: same function works with any Shape
function printInfo(shape: Shape) {
  console.log(`Area: ${shape.area().toFixed(2)}`);
  console.log(`Perimeter: ${shape.perimeter().toFixed(2)}`);
}

Frequently Asked Questions

OOP vs functional programming?

OOP organizes code around objects with state and behavior. FP organizes around pure functions and immutable data. Most modern code uses both — OOP for structure, FP for data transformations. They're complementary, not competing.

Is OOP still relevant?

Absolutely. Java, C#, Python, TypeScript — the most widely used languages are OOP. Understanding OOP is essential even if you prefer functional patterns.

What are the problems with OOP?

Deep inheritance hierarchies, mutable shared state, and overuse of design patterns. Modern OOP addresses these with composition, immutability, and simpler designs.