Inheritance

1. Explain inheritance in Python and its main use case.
Inheritance is a technique to create a new class (child class) that has access (inherited) attributes and methods from other classes (parent class).
2. Describe the use cases of inheritance.
  • Code Reusability: Inheritance promotes code reuse by allowing a new class to inherit attributes and methods from an existing class. This reduces code redundancy and makes the code more modular.
  • Inherit: Child class can use methods and attributes from the parent class.
  • Extend: Child class can define additional functions and methods specific to the subclass use-case.
  • Override: Within the child class, we can modify the parent class’s methods and attribute values.
3. What happens to a child class when parent class methods are altered, assuming no override?
The modifications to the method in the parent class will affect the behaviour of instances of the child class.
4. What happens to a child class when parent class methods are altered, assuming overriding occurred?
If the method in the parent class is overridden in the child class (i.e., the child provides its own implementation of the method), changes to the method in the parent class do not directly impact the child class.
5. If a class does not explicitly inherit from any other class, what does it implicitly inherits?
If a class does not explicitly inherit from any other class, it implicitly inherits from the built-in class called object. In Python, all classes are subclasses of the object class by default.
6. Explain the use case of the isinstance() function in terms of inspecting the class.
isinstance() is a built-in function used to check whether an object is an instance of a particular class. Do note that isinstance() can inspect the highest level of the parent class (root).
7. Explain the use case of the type() function in terms of inspecting the class.
It is a built-in function used to determine the type of an object. It is commonly used to check the class that the instance was created from.
8. Explain why isinstance() is used more than type() when examining a class.
We are often more concerned whether a certain object has certain behaviours and isinstance() is able to check whether the object is an instance of the parent class, making it easier to know the behaviours. In contrast type() can only provide the class that the object was instantiated from.
9. What is the recommended practice in using isinstance() to evaluate the object’s available behaviours?
Use the least restrictive parent class.
10. Explain the use case of the issubclass() function.
issubclass() is used to inspect inheritance relationships between classes (not instances). It can inspect the highest level of the parent class (root).
11. Explain the use case of the super() function.
The primary use of super() is to call a method in the parent class from a subclass.
12. Is super() limited to direct parent class?
No, if it does not find the method in the direct parent class, it will search for the method from classes higher up in the inheritance hierarchy.
Last updated on 21 Nov 2023