๐ Inheritance in Python (Made Simple)
Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows a class to use the properties and methods of another class.
Inheritance allows us to define a class that inherits all the methods and properties from another class.
Think of it like a family: a child can inherit features from their parents.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class.
In programming, a class (child) can inherit features (methods and variables) from another class (parent).
For more on Python classes, check out our post on Python Classes and Objects.
✅ Why Use Inheritance?
-
To reuse code.
-
To make the program easier to manage.
-
To follow the DRY principle (Don't Repeat Yourself).
๐งฑ Basic Syntax
class Parent:
# parent class
pass
class Child(Parent):
# child class that inherits from Parent
pass
๐ Example: Simple Inheritance
# Parent class
class Animal:
def speak(self):
print("I am an animal")
# Child class
class Dog(Animal):
def bark(self):
print("Woof!")
# Creating object of Dog
my_dog = Dog()
my_dog.speak() # Inherited from Animal
my_dog.bark() # Defined in Dog
๐ Output:
I am an animal
Woof!
Here, the Dog
class inherits the speak()
method from the Animal
class. It also has its own method called bark()
.
๐ Example 2: Inheriting from a Person Class
Let’s create a simple example where a Student
class inherits from a Person
class.
๐จ๐ฉ๐ง๐ฆ Step 1: Create the Parent Class
# Parent class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
# Creating an object of Person
x = Person("Steve", "Tendulkar")
x.printname()
๐ Output:
Steve Tendulkar
๐ง Step 2: Create the Child Class
# Child class that inherits from Person
class Student(Person):
pass # No additional code, just inheriting everything from Person
# Creating an object of Student
x = Student("Mike", "Olsen")
x.printname()
๐ Output:
Mike Olsen
๐ Note:
The pass
keyword is used when you don't want to add any extra functionality to the child class. The Student
class automatically gets all the properties and methods from the Person
class.
๐ Types of Inheritance in Python
-
Single Inheritance – One child inherits from one parent.
-
Multiple Inheritance – One child inherits from multiple parents.
-
Multilevel Inheritance – A child inherits from a parent, and another child inherits from that child.
-
Hierarchical Inheritance – Multiple children inherit from one parent.
-
Hybrid Inheritance – A mix of any of the above types.
๐งช Example: Multiple Inheritance
class Father:
def skill(self):
print("Good at driving")
class Mother:
def talent(self):
print("Good at cooking")
class Child(Father, Mother):
def hobby(self):
print("Loves painting")
c = Child()
c.skill()
c.talent()
c.hobby()
๐ Output:
Good at driving
Good at cooking
Loves painting
๐ก Important Points
-
The
super()
function is used to call methods from the parent class. -
Inheritance makes your code organized and reusable.
-
If both parent and child have the same method, the child’s method will override the parent’s method.
๐ Using super()
Example
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
super().speak()
print("Dog barks")
d = Dog()
d.speak()
๐ Output:
Animal speaks
Dog barks
๐ฏ Conclusion
Inheritance helps us write cleaner and more efficient code. Once we understand it, we can build complex applications more easily by reusing and extending existing code.
For more on Python classes, check out our post on Python Classes and Objects.
No comments:
Post a Comment