Table of contents
Introduction
Welcome to my 78th blog post on the Python journey. On day 78, I learned about the simplest and the most common form of inheritance which is SINGLE INHERITANCE in PYTHON. Here the class can inherit attributes and properties from its parent class. Let's dive into more details and understand SINGLE INHERITANCE in Python.
So let's get started......
Single Inheritance
Here a class inherits properties and behaviors from a single-parent class.
This is the simplest and most common form of inheritance.
Syntax -
class ChildClass(ParentClass):
# class body
To create a new class that inherits from a parent class, simply specify the parent class in the class definition, inside the parentheses, as shown above
Example -
Consider a class named "Animal" that contains the attributes and behaviors that are common to all animals.
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
def make_sound(self):
print("Sound made by the animal")
We create a new class for a specific type of animal, such as a dog, that inherits from the Animal class.
class Dog(Animal):
def __init__(self, name, breed):
Animal.__init__(self, name, species="Dog")
self.breed = breed
def make_sound(self):
print("Bark!")
d = Dog("Dog", "Doggerman")
d.make_sound()
a = Animal("Dog", "Dog")
a.make_sound()
Output
Bark!
Sound made by the animal
Explanation : - The Dog class is the child class that inherits all the attributes and behaviors of the parent class Animal class, including the __init__
method and the make_sound
method. Additionally, the Dog class has its own __init__
method constructor that adds a new attribute for the dog's breed, and it also overrides the make_sound
method to specify the unique sound that a dog makes i.e ''bark".
Resources Used
You can watch the video of Day#78 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 78, I learned about Single inheritance which allows us to create new classes based on existing classes. Single inheritance in Python helps us to reuse the code and extend the functionality to fit our custom needs, making it easier to manage complex systems.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #78
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.