Open In App

Create Derived Class from Base Class Universally in Python

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In object-oriented programming, the concept of inheritance allows us to create a new class, known as the derived class, based on an existing class, referred to as the base class. This facilitates code reusability and structuring. Python provides a versatile way to create derived classes from base classes universally, allowing for flexibility and scalability in your code.

Syntax

To create a derived class from a base class universally in Python, we use the following syntax: In this syntax, DerivedClass is created by mentioning BaseClass in parentheses. This implies that DerivedClass inherits the attributes and methods of the BaseClass

class BaseClass:

# Base class attributes and methods

class DerivedClass(BaseClass):

# Additional attributes and methods specific to the derived class

Advantages :

  • Code Reusability: Universal inheritance enables reuse of code from the base class in derived classes, reducing redundancy.
  • Ease of Maintenance: Modifications to the base class automatically propagate to all derived classes, simplifying code maintenance.
  • Extensibility: Derived classes can build upon and customize functionalities of the base class.
  • Consistent Interface: Provides a uniform interface across related classes, enhancing code readability.
  • Polymorphism: Supports treating objects of derived classes as objects of the base class, promoting flexibility in code usage.

How To Create Derived Class From Base Class Universally In Python?

Simple Inheritance in Python

The below code defines two classes, `Animal` and `Dog`, where `Dog` is derived from `Animal`. It initializes attributes like species and breed, and the `make_sound` method is overridden in the `Dog` class. An instance of `Dog` is created, and its attributes, including those inherited from `Animal`, are accessed and printed, demonstrating the principles of inheritance in Python.

Python3




class Animal:
    def __init__(self, species):
        self.species = species
 
    def make_sound(self):
        pass
 
class Dog(Animal):
    def __init__(self, breed):
        super().__init__("Dog")
        self.breed = breed
 
    def make_sound(self):
        return "Woof!"
 
# Creating an instance of the derived class
my_dog = Dog("Labrador")
 
# Accessing attributes and methods from both base and derived classes
print(f"Species: {my_dog.species}, Breed: {my_dog.breed}, Sound: {my_dog.make_sound()}")


Output

Species: Dog, Breed: Labrador, Sound: Woof!


Universal Inheritance in Python

The below code defines two classes, `Shape` and `Circle`, where `Circle` is derived from `Shape`. It initializes attributes such as color and radius, and the `draw` method is overridden in the `Circle` class. An instance of `Circle` is created, and its attributes, including those inherited from `Shape`, are accessed and printed, showcasing the use of inheritance in Python for code reuse and extension.

Python3




class Shape:
    def __init__(self, color):
        self.color = color
 
    def draw(self):
        pass
 
class Circle(Shape):
    def __init__(self, radius):
        super().__init__("Red")
        self.radius = radius
 
    def draw(self):
        return f"Drawing a red circle with radius {self.radius} units."
 
# Creating an instance of the derived class
my_circle = Circle(5)
 
# Accessing attributes and methods from both base and derived classes
print(f"Color: {my_circle.color}, Draw: {my_circle.draw()}")


Output

Color: Red, Draw: Drawing a red circle with radius 5 units.


Conclusion

In conclusion , Creating derived classes from base classes universally in Python is a powerful concept that enhances the organization and extensibility of your code. It allows you to build upon existing functionalities, promoting a modular and scalable design. By understanding the syntax and examples provided, you can leverage universal inheritance to create a well-structured and efficient object-oriented Python codebase.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads