Method overriding

Python Practice › OOP Programs › Program 14

Method overriding

EasyOOP Programs · 14 of 48

Write a Python program that demonstrates method overriding.

Output
Sending: Hi
Sending: Hi (via email)

Approach

  1. The child defines a method with the same name; super() reuses the parent’s version.

Solution

Python
class Notification:    def send(self, msg):        return f"Sending: {msg}" class EmailNotification(Notification):    def send(self, msg):        return super().send(msg) + " (via email)" print(Notification().send("Hi"))print(EmailNotification().send("Hi"))
Output
Sending: Hi
Sending: Hi (via email)

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Method overloading

Python Practice › OOP Programs › Program 13

Method overloading

MediumOOP Programs · 13 of 48

Write a Python program that shows how to achieve method overloading.

Output
5
9
0

Approach

  1. Python keeps only the last method with a given name, so use default and variable arguments instead.

Solution

Python
class Calculator:    def add(self, *numbers):        return sum(numbers) c = Calculator()print(c.add(2, 3))print(c.add(2, 3, 4))print(c.add())
Output
5
9
0

Line by line

Line 2

*numbers accepts any number of arguments, so one method covers every case.

Tip

functools.singledispatchmethod can choose a method by the type of the first argument when the behaviour must differ by type.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Hierarchical inheritance

Python Practice › OOP Programs › Program 12

Hierarchical inheritance

EasyOOP Programs · 12 of 48

Write a Python program that demonstrates hierarchical inheritance.

Output
I am a Square
I am a Circle

Approach

  1. Several children share one parent.

Solution

Python
class Shape:    def describe(self):        return f"I am a {type(self).__name__}" class Square(Shape):    pass class Circle(Shape):    pass for s in [Square(), Circle()]:    print(s.describe())
Output
I am a Square
I am a Circle

Line by line

Line 3

type(self) is the actual child class, so the shared method gives a different answer for each.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Multilevel inheritance

Python Practice › OOP Programs › Program 11

Multilevel inheritance

EasyOOP Programs · 11 of 48

Write a Python program that demonstrates multilevel inheritance.

Output
4 petrol 300

Approach

  1. A grandchild inherits from a child, which inherits from a parent.

Solution

Python
class Vehicle:    def wheels(self):        return 4 class Car(Vehicle):    def fuel(self):        return "petrol" class SportsCar(Car):    def speed(self):        return 300 s = SportsCar()print(s.wheels(), s.fuel(), s.speed())
Output
4 petrol 300

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Multiple inheritance

Python Practice › OOP Programs › Program 10

Multiple inheritance

EasyOOP Programs · 10 of 48

Write a Python program that demonstrates multiple inheritance.

Output
photo taken | calling
['SmartPhone', 'Camera', 'Phone', 'object']

Approach

  1. A class inherits from two parents; the MRO decides lookup order.

Solution

Python
class Camera:    def photo(self):        return "photo taken" class Phone:    def call(self):        return "calling" class SmartPhone(Camera, Phone):    pass s = SmartPhone()print(s.photo(), "|", s.call())print([c.__name__ for c in SmartPhone.__mro__])
Output
photo taken | calling
['SmartPhone', 'Camera', 'Phone', 'object']

Line by line

Line 13

The method resolution order shows Python searches SmartPhone, then Camera, then Phone.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Single inheritance

Python Practice › OOP Programs › Program 9

Single inheritance

EasyOOP Programs · 9 of 48

Write a Python program that demonstrates single inheritance.

Output
eating and barking

Approach

  1. The child class lists one parent class in brackets.

Solution

Python
class Animal:    def eat(self):        return "eating" class Dog(Animal):    def bark(self):        return "barking" d = Dog()print(d.eat(), "and", d.bark())
Output
eating and barking

Line by line

Line 5

Dog(Animal) gives Dog everything Animal has.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Create objects under if __name__ == “__main__”

Python Practice › OOP Programs › Program 8

Create objects under if __name__ == "__main__"

EasyOOP Programs · 8 of 48

Write a Python class and create its object inside the if __name__ == "__main__": block.

Output
Hello, SQA Tools

Approach

  1. Code in that block runs only when the file is executed directly, not when imported.

Solution

Python
class Greeter:    def __init__(self, name):        self.name = name     def greet(self):        return f"Hello, {self.name}"  if __name__ == "__main__":    g = Greeter("SQA Tools")    print(g.greet())
Output
Hello, SQA Tools

Line by line

Lines 9-11

Another file can import Greeter without creating this demo object.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Class and module name

Python Practice › OOP Programs › Program 7

Class and module name

EasyOOP Programs · 7 of 48

Write a Python program to get an object’s class name and module name.

Output
Product
__main__

Approach

  1. type(obj).__name__ and __module__.

Solution

Python
class Product:    pass p = Product()print(type(p).__name__)print(Product.__module__)
Output
Product
__main__

Line by line

Line 6

The module is __main__ because the class is defined in the script being run.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Class method

Python Practice › OOP Programs › Program 6

Class method

EasyOOP Programs · 6 of 48

Write a Python class with a class method used as an alternative constructor.

Output
2 11 2026

Approach

  1. @classmethod receives the class as cls.

Solution

Python
class Date:    def __init__(self, day, month, year):        self.day, self.month, self.year = day, month, year     @classmethod    def from_string(cls, text):        day, month, year = map(int, text.split("-"))        return cls(day, month, year) d = Date.from_string("02-11-2026")print(d.day, d.month, d.year)
Output
2 11 2026

Line by line

Lines 5-8

from_string parses text and calls cls(...), so subclasses get objects of their own type.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Static method

Python Practice › OOP Programs › Program 5

Static method

EasyOOP Programs · 5 of 48

Write a Python class with a static method.

Output
True False

Approach

  1. @staticmethod needs no self; it is a helper grouped with the class.

Solution

Python
class MathUtils:    @staticmethod    def is_even(n):        return n % 2 == 0 print(MathUtils.is_even(10), MathUtils.is_even(7))
Output
True False

Line by line

Lines 2-4

The method uses only its arguments, so it does not need an object.

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs