4.2 Python Classes - inheritance - multiple inheritance

Опубликовано: 30 Март 2026
на канале: World Gurukul way of Learning Values and Wisdom
411
3

Class inheritance • We don’t need to write new code every time, but can create any number of instances of a class. • With inheritance we can add extra features in child instance besides inheriting everything from parent class. • If new attributes or methods have the same name as that of parent class, they are used overriding parent’s. • self is to refer to things in the class from within itself. self is the first parameter in any function defined inside a class.
• Class Shape defines variables - width (x), height (y), and functions - area, perimeter etc. • When instance of Shape is created, _init_ function/method is called which has initialization code.
• We create an instance of a class by calling class name (Shape) and pass arguments. • Arguments pass to the _init_ function and instance (square) is created. • With instance name we can access variables and functions of the parent class. Inheritance Example - Shape
Define a new (child) class square using (parent) Shape class. No need to define shape features again, but inherit from parent.
class DerivedClass(BaseClass):
inheritance
• Derived (child) class gets all attributes and methods from base (parent) class. • In child class, we can add new attributes and methods, overwrite parent class features.
Method Overriding
• Derived classes may override methods of their base classes. • An overriding method in a derived class may in fact want to extend/replace the base class method of the same name. – BaseClass.methodname(self, arguments).
Multiple Inheritance
Python supports multiple inheritance as well. A class definition with multiple base classes is called multiple inheritance class DerivedClass(Base1, Base2, Base3):
MRO: If an attribute is not found in DerivedClass, it is searched for in Base1, then (recursively) in the base classes of Base1, and if it was not found there, it will besearched for in Base2, and so on.
Resolving references
• Resolving attribute references: if a requested attribute is not found in the class, the search proceeds to look in the base class. This rule is applied recursively if the base class itself is derived from some other class. • Resolving Method references: the corresponding class attribute is searched, descending down the chain of base classes if necessary.

Multiple inheritance
• Multiple inheritance is a feature of object-oriented programming language in which an object or class can inherit characteristics and features from more than one parent object or parent class. • In single inheritance, an object or class may only inherit from one particular object or class.
Object class
• All classes inherit from object, so any case of multiple inheritance provides more than one path to reach object. • Return a new featureless object, object is a base for all classes. It has the methods that are common to all instances of Python classes.
Private Variables - name mangling
• Private instance variables: a name prefixed with an underscore (e.g. _var) to hide them outside of the class. • Any identifier of the form __var is replaced with _classname__var, where classname is the current class name with leading underscore(s) stripped. • Name mangling is helpful for letting subclasses override methods without breaking intra-class method calls.
Diamond inheritance problem
The "diamond inheritance problem” is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
Method Resolution Order (MRO)