ZSZainSpireLibrary
Notes · Python · updated 12 Nov 2026

10 OOP concepts every developer should know

Every core idea of object-oriented programming in plain words, each with a short Python example built around one robot. Every example runs as it is: paste it into a file and run python file.py.

The whole picture in one line each Class = blueprint · Object = the real thing · Constructor = setup · Encapsulation = private data · Abstraction = simple button · Inheritance = reuse a parent · Polymorphism = same call, different answer · Overriding = child's version wins · Overloading = same name, different inputs · Interface = a promise · Composition = built from parts.

01Class & ObjectOne blueprint, many objects

A class is the blueprint. An object is one real thing made from it. One Robot class can make as many robots as you want, and each one is separate.

class Robot:                 # the blueprint
    pass

zee = Robot()                # object 1
rex = Robot()                # object 2
print(zee is rex)            # False: two separate objects

02ConstructorSets up each new object

The constructor (__init__ in Python) runs automatically when you create an object. Use it to give each object its starting values, like a name.

class Robot:
    def __init__(self, name):    # runs for every new object
        self.name = name

zee = Robot("Zee")
print(zee.name)                  # Zee

03EncapsulationData hidden behind methods

Encapsulation keeps an object's data private and lets the outside world change it only through methods. Nobody can set the battery to 999 by hand; they have to call charge(). In Python, a double underscore (__battery) hides the attribute by name mangling.

class Robot:
    def __init__(self):
        self.__battery = 25          # private: can't touch it directly

    def charge(self):                # the only way in
        self.__battery = 100

    def battery(self):
        return self.__battery

zee = Robot()
zee.charge()
print(zee.battery())                 # 100

04AbstractionUse it without the details

Abstraction shows a simple button and hides the complicated work behind it. You call start() and never think about motors or loading. A single underscore (_check_motors) tells other developers "internal, don't call this".

class Robot:
    def start(self):                 # simple button for the user
        self._check_motors()
        self._load_brain()
        print("Ready!")

    def _check_motors(self): ...     # hidden details
    def _load_brain(self): ...

Robot().start()                      # Ready!

05InheritanceChild reuses the parent’s code

Inheritance lets a child class reuse everything from a parent class and add its own extras. Robot gets move() from Machine for free and adds talk().

class Machine:
    def move(self):
        print("Moving")

class Robot(Machine):            # Robot extends Machine
    def talk(self):
        print("Beep")

zee = Robot()
zee.move()                       # Moving  (from the parent)
zee.talk()                       # Beep    (its own)

06PolymorphismSame call, different behaviour

Polymorphism: different objects answer the same call in their own way. Your loop just calls speak(); it doesn't care whether it's a robot or a phone.

class Robot:
    def speak(self): return "Beep!"

class Phone:
    def speak(self): return "Ring!"

for thing in [Robot(), Phone()]:
    print(thing.speak())         # Beep!  then  Ring!

07Method OverridingChild replaces the parent’s version

Method overriding: the child writes a method with the same name as the parent's, and the child's version wins. Use super().talk() inside it if you still want the parent's result too.

class Machine:
    def talk(self):
        return "Hello"

class Robot(Machine):
    def talk(self):              # same name, replaces the parent's
        return "Beep boop"

print(Robot().talk())            # Beep boop

08Method OverloadingSame name, different inputs

Method overloading: one method name that works with different inputs. Java and C# let you write several methods with the same name. Python doesn't (the last definition wins), so in Python you get the same effect with default values or *args.

class Robot:
    def move(self, steps=1, direction="right"):
        return f"{steps} steps {direction}"

zee = Robot()
print(zee.move())                # 1 steps right
print(zee.move(5))               # 5 steps right
print(zee.move(5, "left"))       # 5 steps left

09InterfaceA promise every class keeps

An interface is a promise: "every class that signs this must have these methods". Python does it with an abstract base class. Forget to write charge() in a subclass and Python refuses to create the object.

from abc import ABC, abstractmethod

class Chargeable(ABC):           # the promise
    @abstractmethod
    def charge(self): ...

class Robot(Chargeable):
    def charge(self): return "Robot charged"

class Phone(Chargeable):
    def charge(self): return "Phone charged"

for d in [Robot(), Phone()]:
    print(d.charge())

10CompositionObjects built from other objects

Composition builds an object out of other objects ("has-a"), instead of inheriting ("is-a"). A robot has a battery and a brain. Many teams prefer it over deep inheritance because the parts are easy to swap.

class Battery:
    level = 100

class Brain:
    def think(self): return "Thinking..."

class Robot:
    def __init__(self):
        self.battery = Battery()     # Robot HAS-A Battery
        self.brain = Brain()         # Robot HAS-A Brain

zee = Robot()
print(zee.brain.think(), zee.battery.level)
Interview tipBe ready for two classic questions: overriding vs overloading (overriding = child replaces a parent method at runtime; overloading = same name with different parameters, and Python fakes it with default arguments) and abstraction vs encapsulation (abstraction hides how it works; encapsulation protects the data).