
Python Inheritance: A Deep Dive
Python Inheritance
One of the fundamentals of object-oriented programming (OOP) is inheritance. It improves program modularity, makes complicated systems simpler, and permits code reuse. You can make new classes based on preexisting ones by utilizing inheritance, which allows you to inherit their characteristics and actions without having to start from scratch.
In Python, inheritance allows a child class to access and utilize the properties and methods of a parent class. Additionally, a child class can override or extend the functionality of the parent class, giving it the flexibility to define its own unique behavior.
Let’s explore the details of inheritance in Python with practical examples.
Complete Python Course with Advance topics:- CLICK HERE
Basics of Python Inheritance
Syntax
To inherit from a base class, just put the name of the base class in parenthesis after the name of the derived class:
class DerivedClass(BaseClass):
<class-suite>
A class can inherit from more than one parent class thanks to Python’s support for multiple inheritance:
class DerivedClass(BaseClass1, BaseClass2, ..., BaseClassN):
<class-suite>
Single Inheritance Example
A child class inherits from a single parent class in a single inheritance scenario.
class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal): # Dog inherits from Animal
def bark(self):
print("Dog Barking")
d = Dog()
d.bark()
d.speak()
Output:
Dog Barking
Animal Speaking
Multi-Level Inheritance
Multi-level inheritance involves a chain of classes where a derived class inherits from another derived class.
Syntax:
class Class1:
<class-suite>
class Class2(Class1):
<class-suite>
class Class3(Class2):
<class-suite>
Example:
class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal):
def bark(self):
print("Dog Barking")
class DogChild(Dog):
def eat(self):
print("Eating Bread")
d = DogChild()
d.bark()
d.speak()
d.eat()
Output:
Dog Barking
Animal Speaking
Eating Bread
Multiple Inheritance
In multiple inheritance, a derived class inherits attributes and methods from more than one parent class.
Syntax:
class Base1:
<class-suite>
class Base2:
<class-suite>
class Derived(Base1, Base2):
<class-suite>
Example:
class Calculation1:
def add(self, a, b):
return a + b
class Calculation2:
def multiply(self, a, b):
return a * b
class Derived(Calculation1, Calculation2):
def divide(self, a, b):
return a / b
d = Derived()
print(d.add(10, 20))
print(d.multiply(10, 20))
print(d.divide(20, 10))
Output:
30
200
2.0
Useful Built-In Methods
issubclass()
The issubclass(sub, sup)
method checks if one class is a subclass of another.
print(issubclass(Derived, Calculation1)) # True
print(issubclass(Calculation1, Calculation2)) # False
isinstance()
The isinstance(obj, class)
method checks if an object is an instance of a specific class.
print(isinstance(d, Derived)) # True
Method Overriding
A child class can redefine a method from the parent class to provide specific behavior.
Example:
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 get_interest_rate(self):
return 10
class SBI(Bank):
def get_interest_rate(self):
return 7
class ICICI(Bank):
def get_interest_rate(self):
return 8
b1 = Bank()
b2 = SBI()
b3 = ICICI()
print("Bank Rate of Interest:", b1.get_interest_rate())
print("SBI Rate of Interest:", b2.get_interest_rate())
print("ICICI Rate of Interest:", b3.get_interest_rate())
Output:
Bank Rate of Interest: 10
SBI Rate of Interest: 7
ICICI Rate of Interest: 8
Data Abstraction and Hiding
In Python, data hiding is achieved by prefixing an attribute with double underscores (__
). This makes the attribute private to the class.
Example:
class Employee:
__count = 0 # Private attribute
def __init__(self):
Employee.__count += 1
def display(self):
print("Number of employees:", Employee.__count)
emp1 = Employee()
emp2 = Employee()
try:
print(emp1.__count) # This will raise an AttributeError
except AttributeError as e:
print(e)
finally:
emp1.display()
Output:
'Employee' object has no attribute '__count'
Number of employees: 2
Inheritance in Python is a powerful feature that fosters code reuse, modularity, and flexibility. Whether you’re building a simple system or tackling a complex problem, understanding inheritance can help you design more efficient and maintainable applications.
Python Classes and Objects: A Guide to Mastering Object-Oriented Programming |
https://updategadh.com/python/python-classes-and-objects/ |
Python Classes and Objects: A Guide to Mastering Object-Oriented Programming |
https://updategadh.com/python/python-classes-and-objects/ |
Python OOPs Concepts: A Complete Guide |
https://updategadh.com/python/python-oops-concepts-a-complete-guide/ |
Finding the Second Largest Number in Python |
https://updategadh.com/python/finding-the-second-largest-number/ |
Python SimpleImputer Module: A Comprehensive Guide |
https://updategadh.com/python/python-simpleimputer-module/ |
Download New Real Time Projects :-Click here
types of inheritance in python
multiple inheritance in python
Python Inheritance
python inheritance super
python inheritance example
single inheritance in python
python inheritance override method
polymorphism in python
hierarchical inheritance in python
inheritance in python
Python Inheritance: A Deep Dive
encapsulation in python
Post Comment