Cyber Services / All lessons / Python Programming / Object-Oriented Programming

Object-Oriented Programming

Module 19 of 31 · 4 min read · Free to read

MODULES ↓ Command blocks have a COPY button
Classes & Objects: From Fundamentals to Mastery

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of attributes (properties) and code in the form of methods (functions). OOP helps organize complex programs by creating reusable, modular components that mirror real-world entities.

Classes and Objects: The Foundation

  • A class is a blueprint or template for creating objects.
  • An object is an instance of a class— a concrete realization of that blueprint.
Creating a Simple Class and Object
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

# Create an object (instance) of the Person class
person1 = Person("John", "Doe")
person1.printname()  # Output: John Doe

The __init__() Method: The Constructor

The __init__() method is a special method in Python classes. It is automatically called when a new object is created from the class. It initializes the object's attributes.

  • Always include self as the first parameter—it refers to the current instance.
  • You can pass additional parameters to customize each object.

Inheritance: Reusing and Extending Code

Inheritance allows a new class (called the child or derived class) to inherit attributes and methods from an existing class (called the parent or base class).

This promotes code reuse and establishes a natural hierarchy among classes.

Basic Inheritance
# Parent class
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

# Child class
class Student(Person):
    pass  # Inherits everything from Person

# Use the Student class
student1 = Student("Mike", "Olsen")
student1.printname()  # Output: Mike Olsen

Adding the __init__() Function to Child Class

When you add an __init__() method to the child class, it overrides the parent's __init__() method. To preserve the parent's initialization, you must explicitly call it.

Calling Parent's __init__()
class Student(Person):
    def __init__(self, fname, lname):
        Person.__init__(self, fname, lname)  # Manual call

Using the super() Function

The super() function is a more elegant and Pythonic way to inherit from the parent class. It automatically refers to the parent class, so you don’t need to name it explicitly.

super() for Cleaner Code
class Student(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)  # No need to mention 'Person'

Adding Properties to the Child Class

You can extend the child class by adding new attributes that the parent class doesn’t have.

Adding a New Property
class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year  # New property

student1 = Student("Mike", "Olsen", 2019)
print(student1.graduationyear)  # Output: 2019

Adding Methods to the Child Class

Child classes can also define new methods or override existing ones from the parent class.

Adding a New Method
class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year

    def welcome(self):
        print("Welcome", self.firstname, self.lastname, 
              "to the class of", self.graduationyear)

student1 = Student("Mike", "Olsen", 2019)
student1.welcome()  
# Output: Welcome Mike Olsen to the class of 2019

Method Overriding

If a child class defines a method with the same name as a method in the parent class, the child’s version overrides the parent’s. This is a core feature of polymorphism.

Overriding a Parent Method
class Person:
    def introduce(self):
        return "I am a person."

class Student(Person):
    def introduce(self):
        return "I am a student."

s = Student()
print(s.introduce())  # Output: I am a student.

Key Benefits of OOP

  • ✅ Modularity: Code is organized into self-contained classes.
  • ✅ Reusability: Inheritance lets you reuse code without duplication.
  • ✅ Maintainability: Changes to one class rarely affect others.
  • ✅ Scalability: Easy to add new features by extending existing classes.

Best Practices

  • ✅ Always use super() instead of hardcoding the parent class name.
  • ✅ Keep classes focused—follow the Single Responsibility Principle.
  • ✅ Use meaningful class and method names that reflect real-world concepts.
  • ✅ Document your classes with docstrings to explain their purpose.

Common Mistakes to Avoid

  • ❌ Forgetting self in method definitions.
  • ❌ Not calling super().__init__() when adding __init__ to a child class.
  • ❌ Creating deep inheritance chains—prefer composition when possible.
  • ❌ Overriding methods without understanding the parent’s contract.

Summary: The OOP Workflow

  1. Define a parent class with common attributes and methods.
  2. Create a child class that inherits from the parent.
  3. Use super() to initialize inherited attributes.
  4. Add new properties and methods to extend functionality.
  5. Override methods when you need different behavior in the child class.
Modules (31) ← Python Programming