Programming Paradigms

Object-Oriented Programming (OOP)

Core OOP concepts — classes, objects, and the four pillars: encapsulation, abstraction, inheritance, and polymorphism — with Python examples.

  • OOP
  • Programming Paradigms
  • Python
Object-Oriented Programming illustration

What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm built around objects — bundles of data (attributes) and behavior (methods) — rather than functions and logic alone. A class is the blueprint; an object is an instance created from that blueprint.

Class: Car- brand- speed+ accelerate()+ brake()instantiatescar1: Teslaspeed = 0car2: Toyotaspeed = 40Same class,independent state

The Four Pillars

1. Encapsulation

Bundling data and the methods that operate on it inside a single unit (a class), and restricting direct access to some of an object’s internal state.

encapsulation.py

2. Abstraction

Exposing only the essential features of an object while hiding the implementation details — the user of a class doesn’t need to know how a method works, just what it does.

abstraction.py

3. Inheritance

A class (child/subclass) can reuse and extend the attributes and methods of another class (parent/superclass), avoiding duplicated code.

inheritance.py

4. Polymorphism

The same interface (method name) behaves differently depending on the object calling it — as seen above, speak() produces different output for Dog and Cat without the caller needing to know which subclass it’s dealing with.

polymorphism.py

OOP vs Procedural Programming

OOP vs Procedural — quick comparison
aspectproceduralobject-oriented
OrganizationFunctions operating on dataObjects bundling data + behavior
ReusabilityCopy-paste or shared functionsInheritance & composition
Data safetyData usually global/sharedEncapsulation restricts access
ScalingHarder as codebase growsEasier to model complex domains
ExamplesC, shell scriptsPython, Java, C++, C#

Composition over Inheritance

A common modern guideline: prefer composition (an object has-a another object) over deep inheritance chains (an object is-a type of another), since composition keeps classes more flexible and easier to test.

composition.py

Key Takeaways

  • Class = blueprint, Object = instance of that blueprint.
  • Encapsulation protects internal state; Abstraction hides complexity.
  • Inheritance promotes reuse; Polymorphism lets different objects respond to the same call in their own way.
  • Favor composition over inheritance when relationships aren’t a strict “is-a” hierarchy.