Skip to main content
Object-Oriented Programming (OOP) is a programming paradigm that uses “objects” to design software. An object contains attributes (data, properties, variables) and behaviors (methods, functions). You start by defining classes, which act as blueprints for creating objects.
  • Attributes: Variables associated with the object that hold data. For example, a Car object might have attributes like color, model, and year.
  • Methods: Functions associated with the object that define its behavior. For example, a Car object might have methods like start(), stop(), and accelerate().
This section covers all the foundational topics related to Object-Oriented Programming (OOP) in Python, including the four pillars, classes, objects, and the key benefits of OOP.

Classes and Objects

TypeExplanation
ClassA blueprint for creating objects. It defines the attributes and behaviours that the created objects will have.
ObjectAn instance of a class. It contains data and methods defined by the class.

Four Pillars of OOP

Encapsulation

Bundle data (attributes) and methods (behaviours) into a single unit (class). Restricts direct access to some of the object’s components, preventing accidental modification of data. Mainly hiding internal state and implementation details.

Inheritance

A mechanism to create a new class (subclass) that inherits attributes and methods from an existing class (parent class). Allows for code reuse and establishes a relationship between classes.

Polymorphism

The ability to use a single interface to represent different underlying data types. Allows methods to do different things based on the object it is acting upon, via method overriding or method overloading.

Abstraction

Hiding complex internal implementation details and showing only the essential features of the object. Lets developers focus on what to do rather than how to do it.

Benefits of OOP

BenefitExplanation
Code ReusabilityOOP allows for reusing existing code through inheritance, reducing redundancy and improving maintainability.
ModularityBreaks down complex systems into smaller manageable pieces (classes and objects), making code easier to understand, develop, and debug.
Flexibility and ScalabilityOOP allows for easier modifications and extensions to existing code. New features can be added with minimal changes, making it easier to scale applications.
Clear StructureOOP promotes a clear structure for organizing code, making it easier for teams to collaborate.

Build docs developers (and LLMs) love