Design Patterns OOP Concepts

Object-oriented programming (OOP) organizes software around objects instead of loose functions. Every design pattern in this course builds on four ideas: encapsulation, abstraction, inheritance, and polymorphism. A firm grip on these ideas makes each later pattern easy to read and easy to remember.

Classes and Objects

A class works like a house blueprint. The drawing lists rooms, doors, and windows, but nobody lives inside a drawing. An object is a real house built from that blueprint. One blueprint can produce hundreds of houses, and each house keeps its own paint color and furniture.

┌────────────────────┐
│   Class: Car       │   (blueprint)
│   color            │
│   speed            │
│   drive()          │
└─────────┬──────────┘
          │ creates
    ┌─────┴───────────────┐
    ▼                     ▼
┌──────────────┐   ┌──────────────┐
│ myCar        │   │ yourCar      │   (objects)
│ color = red  │   │ color = blue │
│ speed = 40   │   │ speed = 25   │
└──────────────┘   └──────────────┘

Encapsulation

A vending machine hides its motors, coin tray, and wiring behind a simple panel. You press a button and a snack drops out. Encapsulation brings the same idea to code: an object keeps its data private and exposes a few safe methods. Outside code cannot break the inside by accident.

class Counter {
    private int count = 0;          // hidden data

    public void increment() {       // safe door
        count = count + 1;
    }

    public int getCount() {
        return count;
    }
}

Why Patterns Depend on It

Singleton hides its constructor. Builder hides construction steps. Facade hides a messy subsystem. Each of these patterns controls what the outside world can touch.

Abstraction

A TV remote shows a power button and a volume button. Nobody needs to know which circuit board reads the signal. Abstraction describes what an object does and hides how it does the work. Interfaces and abstract classes carry this idea in code.

interface Shape {
    double area();      // what, not how
}

Inheritance

Inheritance lets a child class reuse the data and behavior of a parent class. Because a Dog is an Animal, Dog receives every Animal feature without rewriting it. Developers call this an "is-a" relationship.

        ┌──────────────┐
        │   Animal     │
        │ eat()        │
        │ sleep()      │
        └──────┬───────┘
        ┌──────┴───────┐
        ▼              ▼
   ┌─────────┐    ┌─────────┐
   │  Dog    │    │  Cat    │
   │ bark()  │    │ purr()  │
   └─────────┘    └─────────┘

Deep inheritance chains turn fragile. A change in the top class ripples through every child below it. Experienced designers keep hierarchies shallow.

Polymorphism

The word means "many forms." One call produces different results depending on the object that receives it. A teacher who says "draw yourself" to a circle and a square gets two different drawings.

shapes = [ Circle, Square, Triangle ]

for each shape in shapes:
    shape.draw()
          │
          ├── Circle   → draws a round outline
          ├── Square   → draws four equal sides
          └── Triangle → draws three sides

The calling code never checks the type. New shapes join the list without any change to the loop. Strategy, State, and Command all lean on this power.

Composition Over Inheritance

Composition builds a class from smaller parts. Car holds an Engine object and calls it when needed, since a Car has an Engine. Developers call this a "has-a" relationship. Swapping a petrol engine for an electric one takes one line, while the same swap through inheritance can force a rewrite of the whole family tree.

Inheritance (is-a)            Composition (has-a)

 Engine                        ┌───────┐   holds   ┌────────┐
   ▲                           │  Car  │ ────────▶ │ Engine │
   │                           └───────┘           └────────┘
  Car

Most patterns in this course choose composition. Decorator wraps objects, Strategy holds an algorithm object, and Bridge joins two hierarchies through a reference.

How OOP Ideas Connect to Patterns

OOP IdeaPatterns That Use It
EncapsulationSingleton, Builder, Facade
AbstractionFactory Method, Abstract Factory, Bridge
InheritanceTemplate Method, Composite
PolymorphismStrategy, State, Command, Visitor
CompositionDecorator, Proxy, Adapter

Key Takeaways

  • A class is a blueprint, and an object is a real item built from it.
  • Encapsulation hides data behind safe methods.
  • Abstraction shows what an object does and hides how.
  • Inheritance models "is-a" links, while composition models "has-a" links.
  • Polymorphism lets one call trigger different behavior across many object types.