Practice: Method Overriding
Q1. Override a greeting
Class Person has method greet returning 'Hello'. Create Student(Person) that overrides greet to return 'Hi, I am a student'. Print the greeting from a Student object.
Goal: Override a parent method.
Q2. Shape area
Make a base class Shape with area() returning 0. Create Square(side) and Circle-free Rectangle(w, h) that override area. Print the area of Square(4) and Rectangle(2, 5), one per line.
Goal: Override methods with different logic.
Q3. Extend with super
Create Base with method info() returning 'base'. Create Child whose info() returns the parent's info plus '+child'. Print Child().info().
Goal: Use super() to extend a method.
Q4. Custom __str__
Write class Book with title and author, and override __str__ to return 'Title by Author'. Print Book('Dune', 'Herbert').
Goal: Override special methods.
Q5. Child with extra attribute
Class Vehicle takes wheels in __init__. Create Bike(Vehicle) that sets wheels to 2 through super() and adds a bell attribute set to True. Print the wheels and bell of a Bike, separated by a space.
Goal: Override __init__ correctly.
Q6. Equality by value
Write class Money with an amount. Override __eq__ so two Money objects with the same amount are equal. Print the result of Money(5) == Money(5).
Goal: Override __eq__.
Q7. Which sound?
What does this print?
class Animal:
def sound(self):
return "..."
class Cat(Animal):
def sound(self):
return "Meow"
print(Cat().sound())
print(Animal().sound())Goal: Trace which version runs.
Q8. Inherited only
What is printed?
class A:
def hello(self):
return "A hello"
class B(A):
pass
print(B().hello())Goal: Understand inheritance without override.
Q9. Super chain
What does this print?
class A:
def go(self):
return "A"
class B(A):
def go(self):
return super().go() + "B"
class C(B):
def go(self):
return super().go() + "C"
print(C().go())Goal: Follow super() chains.
Q10. Method on parent object
What is the output?
class P:
def name(self):
return "parent"
class K(P):
def name(self):
return "kid"
objs = [P(), K(), P()]
print([o.name() for o in objs])Goal: Trace dispatch per object.
Q11. MRO names
What does this print?
class A: pass
class B(A): pass
class C(B): pass
print([c.__name__ for c in C.__mro__])Goal: Read the MRO.
Q12. Overload by redefinition
What does this print?
class T:
def f(self):
return 1
def f(self):
return 2
print(T().f())Goal: Know Python's lack of overloading.
Q13. Default __init__ missing
This code crashes with AttributeError. Fix it so it prints 'Rex 3'.
class Pet:
def __init__(self, name):
self.name = name
class Dog(Pet):
def __init__(self, name, age):
self.age = age
d = Dog("Rex", 3)
print(d.name, d.age)Goal: Fix a missing super call.
Q14. super without parentheses
This code fails. Fix it so it prints 'hi there'.
class A:
def say(self):
return "hi"
class B(A):
def say(self):
return super.say() + " there"
print(B().say())Goal: Fix super usage.
Q15. Missing return
The child prints 'None' instead of 'base-child'. Fix it.
class Base:
def label(self):
return "base"
class Child(Base):
def label(self):
super().label() + "-child"
print(Child().label())Goal: Fix a missing return.
Q16. Overriding vs overloading
Explain in two sentences the difference between method overriding and overloading, and say which one Python supports directly.
Goal: Distinguish the two ideas.
Q17. Why call super()?
Give one reason to call super().__init__() inside a child's __init__.
Goal: Explain the use of super().
Q18. Notification system
Application: class Notifier has send(msg) that returns 'Notify: ' + msg. EmailNotifier and SmsNotifier override send to prefix 'Email: ' and 'SMS: '. Loop over both and print each result for 'Hi'.
Goal: Apply overriding to a real design.