Paradigm Deep Dive · 02 of 04

Object-Oriented Programming

Bundle data and behavior together into objects that model the real-world things your software cares about — the dominant style of enterprise software for 30+ years.

← Back to Foundations
Foundations

What Is OOP?

Instead of "do step A, then step B" you say "here's a Customer; ask it to place an order." The world is modeled as objects sending messages.

Basic Concepts

  • Class: a blueprint for an object — defines fields and methods.
  • Object: a concrete instance of a class, with its own state.
  • Method: a function attached to a class that operates on its data.
  • Constructor: special method that builds new instances.
  • this / self: the current object, inside its own methods.
Pillars

The Four Pillars of OOP

1
Encapsulation

Hide internal state behind a public interface. Other code talks to the object through methods, never by poking its fields.

2
Abstraction

Expose what an object does, hide how. A List has add(); you don't care if it's an array or a linked list inside.

3
Inheritance

A new class extends an existing one, reusing fields and methods. SavingsAccount extends Account.

4
Polymorphism

One interface, many implementations. Shape.area() works on Circle, Square, Triangle — caller doesn't care which.

Building Blocks

OOP Mechanics

Inheritance vs Composition

Inheritance creates an "is-a" relationship (Dog is an Animal). Composition creates a "has-a" relationship (Car has an Engine). Modern advice: favor composition over inheritance — deep inheritance trees become brittle.

Interfaces & Abstract Classes
  • Interface: a contract — "anything with these methods can be used here." No implementation.
  • Abstract class: partial implementation; subclasses fill in the missing methods.
  • Interfaces enable polymorphism without inheritance — the foundation of testable, swappable code.
Access Modifiers
ModifierVisible to
publicAnyone.
protectedThe class and its subclasses.
privateOnly the class itself.
package / internalCode in the same package or assembly.
SOLID Principles
  • S — Single Responsibility: one reason to change per class.
  • O — Open/Closed: open to extension, closed to modification.
  • L — Liskov Substitution: subtypes must work where the base type is used.
  • I — Interface Segregation: many small interfaces beat one big one.
  • D — Dependency Inversion: depend on abstractions, not concretes.
Common Design Patterns
Creational

Singleton, Factory, Builder, Prototype, Abstract Factory.

Structural

Adapter, Decorator, Facade, Proxy, Composite, Bridge.

Behavioral

Strategy, Observer, Command, Iterator, State, Template Method.

Enterprise

Repository, Unit of Work, DTO, Dependency Injection.

An Example (Java)
interface Shape {
    double area();
}

class Circle implements Shape {
    private final double radius;
    Circle(double radius) { this.radius = radius; }

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

class Square implements Shape {
    private final double side;
    Square(double side) { this.side = side; }

    public double area() { return side * side; }
}

// Polymorphism in action — one method, many shapes.
double totalArea(List<Shape> shapes) {
    return shapes.stream().mapToDouble(Shape::area).sum();
}
Languages

OOP Languages

LanguageStyleNotes
SmalltalkPureEverything is an object; the original.
JavaClass-basedThe enterprise standard. Strongly typed.
C#Class-basedMicrosoft's flagship — modern, multi-paradigm.
C++Multi-paradigmAdds OOP to C; multiple inheritance allowed.
PythonMulti-paradigmClasses are first-class; duck typing rules.
RubyPureEverything (numbers included) is an object.
Kotlin / SwiftModernOOP with strong functional features baked in.
Trade-offs

Strengths & Weaknesses

Strengths
  • Models real-world domains intuitively.
  • Encapsulation makes large codebases manageable.
  • Polymorphism enables flexible, swappable designs.
  • Massive ecosystem of patterns, frameworks, and tooling.
Weaknesses
  • Deep inheritance hierarchies become rigid and confusing.
  • Mutable shared state makes concurrency hard.
  • Easy to over-engineer with patterns and abstractions.
  • Boilerplate-heavy in classical languages (Java, C#).
When to Use

Sweet Spots

Enterprise Business Apps

Banking, insurance, ERP — domains rich in nouns and rules.

UI Frameworks

Widgets, controls, event handlers map cleanly to objects.

Game Engines

Entities, components, behaviors form natural object hierarchies.

Long-Lived Codebases

Encapsulation pays compound interest over years of maintenance.

Continue

Other Paradigm Deep Dives