Back to website
Mayank Aggarwal Learning Center

Phase 02 · Python FoundationsLesson 6 of 8

Object-Oriented Programming

Classes, objects, inheritance and the four pillars of OOP.

  • 4:47:53 video
  • 3 min read
  • Intermediate
  • Deep dive
01

The problem

As programs grow, keeping data and the code that works on that data in separate places becomes messy. Object-Oriented Programming (OOP) bundles them together. A BankAccount knows its own balance and how to deposit and withdraw. Almost every serious Python library, including the AI agent frameworks later in this roadmap, is built with classes, so OOP is essential reading.

02

The concept

Deep dive: 4h 48m video. You don’t need it all in one sitting. Use the chapter list under the video to jump straight to the part you need.

Jump to a chapter 29 chapters
  1. 0:00:00Intro
  2. 0:03:20Intro to Functional Programming
  3. 0:07:12Why OOPS
  4. 0:16:11Component of OOPS
  5. 0:22:13Intro to Class and Object
  6. 0:33:40Constructor
  7. 0:40:04Self Keyword
  8. 0:49:56Creating Properties/Data Members
  9. 1:01:34Creating methods
  10. 1:10:30Complex Number Class Code
  11. 1:23:20__str__ method
  12. 1:33:20__add__ method
  13. 1:43:40__mul__ method
  14. 1:47:46__truediv__ method
  15. 1:53:30__eq__ method
  16. 1:59:32Instance variable
  17. 2:00:27Static Property
  18. 2:09:31Encapsulation in OOPS
  19. 2:13:15Access Modifier in Python
  20. 2:26:04Static property
  21. 2:38:11Inheritance in OOPS
  22. 2:57:00super() keyword
  23. 3:09:00Types Of Inheriatance
  24. 3:29:31Questions on Inheritance
  25. 3:39:00Polymorphism in OOPS
  26. 3:56:14Abstraction In OOPS
  27. 3:57:47Inheritance Question Again :(
  28. 4:25:51Abstraction Continued
  29. 4:45:33Outro and Please subscribe :)

Chapters from the video description on YouTube.

Classes and objects#

A class is a blueprint. An object (or instance) is a real thing built from that blueprint.

Python
class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner        # attribute
        self.balance = balance    # attribute

    def deposit(self, amount):    # method
        self.balance += amount
        return self.balance

acct = BankAccount("Ada", 100)    # create an object
acct.deposit(50)                  # 150
print(acct.owner)                 # Ada
  • __init__ runs automatically when an object is created and sets its starting attributes.
  • self is the object itself. Inside a method, self.balance means "this account's balance".
  • Functions defined inside a class are called methods.

Encapsulation: protect your data#

Methods control how data can change, so rules live in one place:

Python
    def withdraw(self, amount):
        if amount > self.balance:
            raise ValueError("Insufficient funds")
        self.balance -= amount

Now nobody can accidentally drive the balance below zero, because the class enforces the rule.

Inheritance: reuse and extend#

A child class inherits everything from a parent class and can add or change behaviour:

Python
class SavingsAccount(BankAccount):
    def __init__(self, owner, balance=0, rate=0.04):
        super().__init__(owner, balance)   # reuse the parent's setup
        self.rate = rate

    def add_interest(self):
        self.balance += self.balance * self.rate

s = SavingsAccount("Ravi", 1000)
s.deposit(500)       # inherited from BankAccount
s.add_interest()     # new behaviour

Parent and child class

BankAccountparent: owner, balance, deposit(), withdraw()
  • SavingsAccountchild: inherits everything from BankAccount
    • Reuses the parent's setupsuper().__init__(owner, balance)
    • Adds new behaviourrate and add_interest()

The four pillars#

PillarIn one line
EncapsulationKeep data and the methods that change it together, behind clear rules
AbstractionExpose what an object does, hide how it does it
InheritanceBuild new classes on top of existing ones
PolymorphismDifferent classes respond to the same method call in their own way

Why this matters for AI work#

In frameworks such as Google ADK and Microsoft AutoGen, agents, tools and model clients are objects. You'll create instances, configure their attributes and sometimes subclass them to customise behaviour. Being fluent with classes makes those frameworks feel familiar instead of magical.

03

Try it

Your task · about 10 minutes

Create a BankAccount class with deposit and withdraw methods that refuse to go below zero. Then make SavingsAccount inherit from it and add interest.

04

Recap & next

Key takeaways

  1. A class is a blueprint; an object is an instance created from it.
  2. __init__ runs when an object is created and sets its attributes; self refers to that object.
  3. Inheritance lets a child class reuse and extend a parent class.
  4. The four pillars: encapsulation, abstraction, inheritance and polymorphism.
  5. Frameworks like AutoGen and ADK are class-based, so OOP is essential for agent work.

Quick check

2 questions · instant feedback

1What is __init__ used for?
2Inheritance lets a class…

Finished this lesson?

Mark it complete to track your progress and keep your streak going.

Next