Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1Python Inheritance
Inheritance is a powerful feature of object-oriented programming (OOP) that allows you to create new classes based on existing classes. It enables code reuse, promotes modularity, and facilitates the creation of a class hierarchy. In this article, we'll explore the concept of inheritance in Python and provide examples to help you understand its implementation and benefits.
Example 1: Creating a Parent Class
Let's start with a basic example of creating a parent class and a child class that inherits from it:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("The", self.name, "makes a sound.")
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
print("The", self.name, "barks.")
dog = Dog("Buddy", "Labrador Retriever")
dog.speak() # Output: The Buddy barks.
In this code, we define a parent class called "Animal" that has an initializer method and a "speak" method. We then create a child class called "Dog" that inherits from the Animal class using the syntax "class Dog(Animal)". The Dog class has its own initializer method that calls the parent class's initializer using the "super()" function. It also has its own implementation of the "speak" method. We create an instance of the Dog class and call the "speak" method on it.
Example 2: Method Overriding
Inheritance allows child classes to override methods inherited from the parent class. Here's an example:
class Shape:
def __init__(self, color):
self.color = color
def draw(self):
print("Drawing a", self.color, "shape.")
class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius
def draw(self):
print("Drawing a", self.color, "circle with radius", self.radius)
shape = Shape("red")
shape.draw() # Output: Drawing a red shape.
circle = Circle("blue", 5)
circle.draw() # Output: Drawing a blue circle with radius 5.
In this example, we define a parent class called "Shape" with an initializer method and a "draw" method. We create a child class called "Circle" that inherits from the Shape class and adds its own initializer method and draw method. We create instances of both the Shape and Circle classes and call the "draw" method on each of them, demonstrating method overriding.
Example 3: Multiple Inheritance
Python supports multiple inheritance, where a class can inherit from multiple parent classes. Here's an example:
class A:
def method(self):
print("Method from class A.")
class B:
def method(self):
print("Method from class B.")
class C(A, B):
pass
c = C()
c.method() # Output: Method from class A.
In this code, we define two parent classes, A and B, each with their own "method" implementation. We then create a child class called C that inherits from both A and B. When we create an instance of C and call the "method" method, it calls the method from class A, as it is the first class in the inheritance list.
Conclusion
Inheritance is a powerful mechanism in Python that promotes code reuse and modularity. By understanding how to create parent and child classes, override methods, and utilize multiple inheritance, you can design more flexible and extensible code. Experiment with the examples provided and explore the possibilities of inheritance in your Python projects.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners