Inheritance & Method Overriding
Inheritance & Method Overriding in Python
Inheritance is a cornerstone of Object-Oriented Programming that enables a child (derived) class to inherit attributes and methods from a parent (base) class. It facilitates clean code reuse, models hierarchical domain relationships ("is-a"), and allows subclasses to specialize or override behaviors.
1. Single Inheritance Basics
In Python, inheritance is declared by passing the parent class name inside parentheses after the child class definition:
2. Extending Constructors with super().__init__()
When a child class needs its own unique attributes in addition to those of the parent, use the built-in super() function to invoke the parent class's constructor:
3. Method Overriding
Method Overriding occurs when a child class provides a custom implementation of a method that is already defined in its parent class.
Full Override vs. Cooperative Augmentation
- 1Full Override: The child method completely replaces the parent method logic.
- 2Augmentation: The child method runs custom logic before or after calling
super().method_name().
4. Multiple Inheritance & Method Resolution Order (MRO)
Python supports multiple inheritance, where a class inherits from two or more parent classes. The order in which methods are resolved is dictated by the C3 Linearization algorithm, accessible via the mro() method or __mro__ attribute.
5. Type Introspection: isinstance() vs. issubclass()
Always use Python's built-in inspection functions rather than comparing raw type(obj) == Class:
Multiple Choice Questions
1. Which function is used in a child class constructor to call the parent class's __init__ method?
A. parent().__init__() B. super().__init__() C. this.__init__() D. base().__init__() Answer: B Explanation: super() returns a proxy object that delegates method calls to the parent or sibling classes according to the class MRO.
2. What happens when a child class defines a method with the exact same name and signature as its parent class?
A. A NameConflictError is raised at compilation B. The child class method overrides the parent class method C. Both methods are merged together automatically D. Python ignores the child method and defaults to the parent Answer: B Explanation: Defining a method in a child class with the same name as a parent method overrides the parent implementation when invoked on instances of the child.
3. What does MRO stand for in Python's object model?
A. Method Return Object B. Main Runtime Order C. Method Resolution Order D. Memory Reference Optimization Answer: C Explanation: MRO stands for Method Resolution Order, the deterministic order in which Python searches class hierarchies for attributes and methods.
4. Which built-in function checks whether a specific class inherits from another class?
A. isinstance() B. issubclass() C. hasattr() D. isderived() Answer: B Explanation: issubclass(sub, sup) returns True if sub is a direct or indirect subclass of sup.
5. If class Dog(Animal) does not define its own __init__ method, what constructor is executed when running Dog()?
A. Python creates a dummy object without initializing attributes B. The constructor of Animal is automatically called C. A TypeError is raised because constructors are mandatory D. The __init__ method of type is called Answer: B Explanation: If a child class does not declare an __init__ method, Python traverses the MRO and calls the parent's __init__ method automatically.
Polymorphism Basics
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Creating Classes & Objects (Review) | Polymorphism Basics |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.