Inheritance represents a crucial element of the object-oriented programming paradigm. It facilitates code reusability within a program, as it allows developers to utilize an existing class to develop a new class rather than constructing one entirely from the ground up.
In the concept of inheritance, the subclass inherits attributes and is able to utilize all the data members and methods that are defined in the superclass. Additionally, a subclass has the ability to offer its own unique implementations for the methods inherited from its parent class. This segment of the tutorial will delve into the intricacies of inheritance comprehensively.
In Python, a subclass can obtain properties and methods from a parent class by simply specifying the parent class name within parentheses following the subclass name. The syntax to inherit from a base class in a derived class is as follows:
Python Inheritance Syntax
It has the following syntax:
class derived-class(base class):
<class-suite>
A class has the capability to inherit from multiple classes by listing each of them within the parentheses. Take a look at the syntax provided below.
Syntax
class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
<class - suite>
Python Inheritance Example
Let’s consider an example to illustrate the concept of inheritance in Python.
class Animal:
def speak(self):
print("Animal Speaking")
#child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
d = Dog()
d.bark()
d.speak()
Output:
dog barking
Animal Speaking
Python Multi-Level inheritance
In Python, multi-level inheritance is achievable, similar to many other object-oriented programming languages. This form of inheritance occurs when a derived class extends the functionality of another derived class. Python does not impose any restrictions on the number of levels that can be reached through multi-level inheritance.
Python Multi-level Inheritance Syntax
It has the following syntax:
class class1:
<class-suite>
class class2(class1):
<class suite>
class class3(class2):
<class suite>
.
.
Python Multi-level Inheritance Example
Let us consider an example to illustrate the concept of multi-level inheritance in Python.
class Animal:
def speak(self):
print("Animal Speaking")
#The child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
#The child class Dogchild inherits another child class Dog
class DogChild(Dog):
def eat(self):
print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()
Output:
dog barking
Animal Speaking
Eating bread...
Python Multiple inheritance
Python offers the capability to derive a child class from multiple parent classes, allowing for multiple inheritance.
Python Multiple Inheritance Syntax
It has the following syntax:
class Base1:
<class-suite>
class Base2:
<class-suite>
.
.
.
class BaseN:
<class-suite>
class Derived(Base1, Base2, ...... BaseN):
<class-suite>
Python Multiple Inheritance Example
To demonstrate the concept of multiple inheritance in Python, let's consider an example.
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5
The issubclass(sub,sup) method
The method issubclass(sub, sup) is utilized to evaluate the relationships between designated classes. It yields a true value if the initial class is a subclass of the second class, and returns false in all other situations.
Python issubclass(sub, sup) method Example
To illustrate the use of the issubclass(sub, sup) function in Python, let’s consider an example.
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(issubclass(Derived,Calculation2))
print(issubclass(Calculation1,Calculation2))
Output:
True
False
The isinstance (obj, class) method
The isinstance function serves to verify the association between objects and their respective classes. It outputs true if the initial argument, referred to as obj, is indeed an instance of the second argument, designated as class.
Python isinstance (obj, class) Method Example
To illustrate the usage of the isinstance(obj, class) method in Python, we will consider an example.
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(isinstance(d,Derived))
Output:
Method Overriding
In our child class, we have the capability to offer a particular implementation of a method that originates from the parent class. This practice is referred to as method overriding. Method overriding becomes necessary in situations where the child class requires a distinct definition of a method that is already defined in the parent class.
Python Method Overriding Example
Take a look at the subsequent example to demonstrate the concept of method overriding in Python.
class Animal:
def speak(self):
print("speaking")
class Dog(Animal):
def speak(self):
print("Barking")
d = Dog()
d.speak()
Output:
Barking
Real Life Example of method overriding
class Bank:
def getroi(self):
return 10;
class SBI(Bank):
def getroi(self):
return 7;
class ICICI(Bank):
def getroi(self):
return 8;
b1 = Bank()
b2 = SBI()
b3 = ICICI()
print("Bank Rate of interest:",b1.getroi());
print("SBI Rate of interest:",b2.getroi());
print("ICICI Rate of interest:",b3.getroi());
Output:
Bank Rate of interest: 10
SBI Rate of interest: 7
ICICI Rate of interest: 8
Data abstraction in python
Abstraction plays a crucial role in the realm of object-oriented programming. In Python, we can achieve data hiding by prefixing an attribute with a double underscore (___). Once this is done, the attribute will become inaccessible from outside the class through its object.
Python Data Abstraction Example
To illustrate the concept of data abstraction in Python, let's consider an example.
class Employee:
__count = 0;
def __init__(self):
Employee.__count = Employee.__count+1
def display(self):
print("The number of employees",Employee.__count)
emp = Employee()
emp2 = Employee()
try:
print(emp.__count)
finally:
emp.display()
Output:
The number of employees 2
AttributeError: 'Employee' object has no attribute '__count'