top of page

Python's Object Oriented Programming (OOP)


Python’s Object-Oriented Programming (OOP) model follows the same timeless principles that have governed structured software design for decades: break big problems into smaller real-world entities, give each entity responsibility, and let them work together through well-defined relationships.

At its core, OOP in Python is built on classes and objects.


1. Class and Object

A class is a blueprint.An object is an instance created from that blueprint.

class Car:    
    def __init__(self, brand, model):        
    self.brand = brand        
    self.model = model    
    def drive(self):        
        return f"{self.brand} {self.model} is driving"

    my_car = Car("Toyota", "Corolla")
    print(my_car.drive())

Here:

  • Car → class

  • my_car → object

  • brand, model → attributes

  • drive() → method

The init() method is the constructor. It runs automatically when an object is created.


2. The Four Pillars of OOP

These are not new ideas — they’ve shaped software engineering since the early days of structured design.


A. Encapsulation

Keep data and behaviour together. Control access to internal state.


class BankAccount:    
    def __init__(self, balance):        
        self.__balance = balance   # private attribute    
    def deposit(self, amount):        
        self.__balance += amount    
    def get_balance(self):        
        return self.__balance

The double underscore __balance prevents direct external modification.


acc = BankAccount(1000)
acc.deposit(500)
print(acc.get_balance())

B. Inheritance

Reuse existing functionality by creating child classes from parent classes.


class Animal:    
    def speak(self):        
        return "Some sound"

class Dog(Animal):    
    def speak(self):        
        return "Bark"

d = Dog()
print(d.speak())

Dog inherits from Animal but overrides the speak() method.


C. Polymorphism

Different classes can use the same method name but behave differently.


class Cat:    
    def speak(self):        
        return "Meow"
class Cow:    
    def speak(self):        
        return "Moo"

animals = [Dog(), Cat(), Cow()]

for animal in animals:    
    print(animal.speak())

Same interface → different implementation.


D. Abstraction

Hide unnecessary details and expose only what is essential.


from abc import ABC, abstractmethod

class Shape(ABC):    
    @abstractmethod    
    def area(self):        
        pass

class Rectangle(Shape):    
    def __init__(self, width, height):        
        self.width = width        
        self.height = height    

    def area(self):        
        return self.width * self.height

You cannot instantiate Shape directly — it forces derived classes to implement area().


3. Class Relationships

Real systems rarely live in isolation.


Composition (Has-A Relationship)


class Engine:    
    def start(self):        
        return "Engine started"

class Vehicle:    
    def __init__(self):        
        self.engine = Engine()    

    def move(self):        
        return self.engine.start()

Vehicle has an Engine.


4. Class vs Instance Variables


class Student:    
    school = "ABC College"   # class variable    

    def __init__(self, name):        
        self.name = name     # instance variable
  • Class variable → shared across all instances

  • Instance variable → unique per object


5. Static and Class Methods


class MathUtil:    
    @staticmethod    
    def add(x, y):        
        return x + y    

    @classmethod    
    def description(cls):        
        return f"This is {cls.__name__}"

Summary

Concept

Meaning

Class

Blueprint

Object

Instance of class

Encapsulation

Protect internal state

Inheritance

Reuse code

Polymorphism

Same interface, diff action

Abstraction

Hide complexity


Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Subscribe to get exclusive updates

Thanks for subscribing!

CONTACT ME
avatar-formal-round.png

Follow

  • Medium
  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
  • Youtube
  • Paypal.Me
  • linktree

© 2019 - 2025 Biyi Akinpelu. The LORD Is My Banner

bottom of page