Practice Python OOP inheritance by identifying and fixing common mistakes in code examples.
Identify and fix the mistake in the following code:
class Father:
def __init__(self, name):
name = name
class Child(Father):
pass
c = Child("Ali")
print(c.name)
Hint: 💡 Remember to use `self` when assigning instance variables inside a constructor.
Issue: The constructor assigns the parameter to itself instead of using `self.name = name`.
class Father:
def __init__(self, name):
self.name = name
Identify and fix the mistake in the following code:
class Animal:
def sound():
print("Some sound")
class Dog(Animal):
pass
d = Dog()
d.sound()
Hint: 💡 Instance methods need to accept the instance (`self`) as the first argument.
Issue: Method `sound()` is missing the `self` parameter.
class Animal:
def sound(self):
print("Some sound")
Identify and fix the mistake in the following code:
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, school):
self.school = school
Hint: 💡 When overriding `__init__`, make sure to call the parent class constructor if needed.
Issue: Child constructor does not call the parent constructor, missing initialization of `name`.
class Student(Person):
def __init__(self, name, school):
super().__init__(name)
self.school = school
Identify and fix the mistake in the following code:
class A:
def show(self):
print("Class A")
class B(A):
def show(self):
print("Class B")
obj = A()
obj.show()
Hint: 💡 Change the code so that an object of `B` calls the `show` method of `A`.
Issue: Method `show()` in class `B` is overriding `A`'s method without calling it.
class B(A):
def show(self):
super().show()
Identify and fix the mistake in the following code:
class Vehicle:
def __init__(self, brand):
self.brand = brand
class Car(Vehicle):
def __init__(self, model):
self.model = model # Error: Forgot super().__init__
car = Car("Tesla", "Model S") # Throws error
Hint: 💡 Add `super().__init__(brand)` in `Car.__init__` to initialize the parent class.
Issue: `Car` doesn't call `Vehicle.__init__`, so the parent class `brand` is not initialized.
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
self.model = model
Identify and fix the mistake in the following code:
class Parent:
def show(self):
print("Parent method")
class Child(Parent):
def show(self):
print("Child method") # Error: Parent method not called
child = Child()
child.show() # Only shows "Child method"
Hint: 💡 Add `super().show()` in `Child`'s `show()` method to preserve the parent method.
Issue: Parent method is not called in `Child`'s overridden method.
class Child(Parent):
def show(self):
super().show()
print("Child method")
Identify and fix the mistake in the following code:
class Parent1:
def display(self):
print("Parent1")
class Parent2:
def display(self):
print("Parent2")
class Child(Parent1, Parent2):
pass # Which display() is called?
obj = Child()
obj.display() # Outputs "Parent1" - is this intended?
Hint: 💡 Change inheritance order to `(Parent2, Parent1)` to control which method is called.
Issue: The method resolution order (MRO) may be confusing when multiple parent classes are involved.
class Child(Parent2, Parent1):
pass
Identify and fix the mistake in the following code:
class Engine:
def start(self):
print("Engine started")
class Car(Engine): # Bad inheritance
def drive(self):
self.start()
car = Car()
car.drive() # Works but poor design
Hint: 💡 Use composition rather than inheritance in this case.
Issue: `Car` class should not inherit from `Engine` as it does not represent an is-a relationship.
class Car:
def __init__(self):
self.engine = Engine()
def drive(self):
self.engine.start()
Identify and fix the mistake in the following code:
class Shape:
def __init__(self, color):
self.color = color
class Circle(Shape):
def __init__(self, radius):
super().__init__() # Error: Missing color arg
self.radius = radius
c = Circle("red", 5) # TypeError
Hint: 💡 Pass `color` argument when calling `super().__init__()`.
Issue: Circle class does not pass the `color` argument when calling `super().__init__()`.
class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius
Identify and fix the mistake in the following code:
name = "BaseClass"
class MyClass(name): # Error: 'str' is not a class
pass
Hint: 💡 You cannot inherit from a non-class type.
Issue: Trying to inherit from a string instead of a class.
class MyClass(object): # Inherit from the base class `object`
pass
Identify and fix the mistake in the following code:
class Base:
def __init__(self):
self.__secret = 123 # Name mangled
class Derived(Base):
def get_secret(self):
return self.__secret # Error: AttributeError
d = Derived()
print(d.get_secret())
Hint: 💡 You cannot directly access a private attribute from a subclass.
Issue: Derived tries to access `__secret`, which is name-mangled.
class Derived(Base):
def get_secret(self):
return self._Base__secret # Access the mangled name
Identify and fix the mistake in the following code:
class A:
def __init__(self):
print("A initialized")
class B(A):
def __init__(self):
super().__init__()
print("B initialized")
class C(A):
def __init__(self):
super().__init__()
print("C initialized")
class D(B, C):
def __init__(self):
super().__init__() # Initializes B -> C -> A
print("D initialized")
d = D() # Output order might surprise beginners
Hint: 💡 Analyze MRO with `print(D.__mro__)`.
Issue: The MRO might cause confusion due to multiple inheritance.
class D(B, C):
def __init__(self):
super().__init__()
print("D initialized")
Identify and fix the mistake in the following code:
class MathOps:
@staticmethod
def add(a, b):
return a + b
class AdvancedMath(MathOps):
def add(a, b, c): # Missing @staticmethod
return a + b + c
result = AdvancedMath.add(1, 2, 3) # TypeError
Hint: 💡 Add `@staticmethod` decorator to `add` method in `AdvancedMath`.
Issue: `AdvancedMath.add` should be a static method but the decorator is missing.
class AdvancedMath(MathOps):
@staticmethod
def add(a, b, c):
return a + b + c
Identify and fix the mistake in the following code:
class Account:
@property
def balance(self):
return self._balance
class Savings(Account):
def balance(self): # Forgot @property
return self._balance * 1.05 # Broken
sa = Savings()
sa.balance # Throws AttributeError
Hint: 💡 Use `@property` decorator in `Savings` to override `balance` method as a property.
Issue: balance becomes a method instead of a property in `Savings`.
class Savings(Account):
@property
def balance(self):
return self._balance * 1.05