Sitemap

Object-Oriented Programming: refresh

4 min readMay 20, 2026

--

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, or objects, rather than functions and logic.

In a Python interview, you need to know the core definitions and the Four Pillars of OOP. Here is a crisp, interview-ready refresher.

Press enter or click to view image in full size

1. The Core Concepts: Classes vs. Objects

  • Class: A blueprint, template, or prototype for creating objects. It defines the attributes (data) and methods (behavior) that the objects will have.
  • Object: An instance of a class. It is a concrete entity created from the blueprint that holds actual data in memory.

Python

# The Blueprint (Class)
class Dog:
def __init__(self, name, breed):
self.name = name # Attribute
self.breed = breed # Attribute
def bark(self): # Method
return f"{self.name} says Woof!"
# The Instance (Object)
my_dog = Dog(name="Buddy", breed="Golden Retriever")
print(my_dog.bark()) # Output: Buddy says Woof!

Key Interview Note: What is self and __init__?

  • __init__ is the constructor method. It runs automatically when you create a new object and initializes the object's attributes.
  • self represents the specific instance of the object you are currently creating or manipulating. It connects the attributes to the specific object.

2. The Four Pillars of OOP

Interviewers love asking: “What are the four pillars of OOP, and how do you implement them in Python?”

Pillar 1: Encapsulation

Encapsulation is the practice of bundling data and methods into a single unit (a class) and restricting direct access to some of the object’s components. This prevents accidental modification of data.

In Python, we achieve this using access modifiers via underscores:

  • _variable: Protected (a warning to other programmers not to touch it outside the class/subclass).
  • __variable: Private (Python actively hides/mangles this variable name so it cannot be accessed directly from outside).

Python

class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance # Private attribute
# Getter method to safely access private data
def get_balance(self):
return self.__balance
# Setter method to safely modify private data with validation
def deposit(self, amount):
if amount > 0:
self.__balance += amount
account = BankAccount("Alice", 1000)
# account.__balance # Throws an AttributeError!
account.deposit(500)
print(account.get_balance()) # Output: 1500

Pillar 2: Inheritance

Inheritance allows a new class (Child/Subclass) to adopt the attributes and methods of an existing class (Parent/Base class). This promotes code reusability.

Get Avithal_DataYoda’s stories in your inbox

Join Medium for free to get updates from this writer.

Python supports both single inheritance and multiple inheritance (a child class inheriting from more than one parent class).

Python

class Employee:  # Parent Class
def __init__(self, name, salary):
self.name = name
self.salary = salary
def work(self):
return f"{self.name} is working."

class Developer(Employee): # Child Class
def __init__(self, name, salary, programming_language):
# super() calls the parent class's __init__
super().__init__(name, salary)
self.programming_language = programming_language
# Overriding the parent's work method
def work(self):
return f"{self.name} is coding in {self.programming_language}."
dev = Developer("Bob", 90000, "Python")
print(dev.work()) # Output: Bob is coding in Python.

Pillar 3: Polymorphism

Polymorphism means “many forms.” It allows different classes to have methods with the same name but different behaviors.

In Python, this is heavily tied to “Duck Typing” (“If it walks like a duck and quacks like a duck, it’s a duck”). Python doesn’t care about the object’s explicit type; it only cares if the method exists.

Python

class Cat:
def make_sound(self):
return "Meow"

class Dog:
def make_sound(self):
return "Woof"

# A function that accepts any object with a 'make_sound' method
def animal_sound(animal_object):
print(animal_object.make_sound())

cat = Cat()
dog = Dog()

animal_sound(cat) # Output: Meow
animal_sound(dog) # Output: Woof

Pillar 4: Abstraction

Abstraction hides complex implementation details and only shows the essential features of an object.

In Python, abstraction is enforced using the abc (Abstract Base Classes) module. You create an abstract class that cannot be instantiated directly, forcing child classes to implement specific methods.

Python

from abc import ABC, abstractmethod

class Vehilcle(abc):
@abstractmethod
def start_engine(self):
pass
class Car(Vehicle):
def start_engine(self):
return ("Vrooom++ varoom")

# my_vehicle = Vehicle() # Throws a TypeError! Cannot instantiate abstract class
my_car = Car()
print(my_car.start_engine()) # Output: Car engine started. Vroom!
class Vehicle(ABC):  # Abstract Class
@abstractmethod
def start_engine(self):
pass
class Car(Vehicle):
def start_engine(self):
return "Car engine started. Vroom!"
# my_vehicle = Vehicle() # Throws a TypeError! Cannot instantiate abstract class
my_car = Car()
print(my_car.start_engine()) # Output: Car engine started. Vroom!

3. Quick-Fire Interview Cheat Sheet

  • super(): Used to call a method from the parent class (most commonly the constructor __init__).
  • Method Overriding: When a child class redefines a method that it inherited from a parent class (as seen in the Developer example).
  • Method Overloading: Python does not support traditional method overloading (having multiple methods with the same name but different parameters in the same class). Instead, Python handles this using default arguments (e.g., def my_method(self, a, b=None)).
  • Class vs. Static Methods:
  • @classmethod: Takes cls as the first argument; can access and modify class-level state.
  • @staticmethod: Takes no implicit first argument; behaves like a plain function isolated inside a class namespace.

--

--