Python Inheritance is a fundamental concept of object-oriented programming that allows developers to reuse existing code and build upon existing functionality. Inheritance enables the creation of new classes that inherit properties and methods from existing classes, called base or parent classes. This feature reduces code duplication, improves code readability and maintainability, and saves time and effort. In this blog, we will discuss Python Inheritance in detail, and provide example code snippets to illustrate its usage.
What is Inheritance in Python?
In Python, inheritance is the mechanism that enables the creation of new classes by inheriting the attributes and behaviors of an existing class. The existing class is called the base or parent class, and the new class is called the derived or child class. The child class can add new attributes and methods, override existing attributes and methods, or inherit them as-is from the parent class.
The syntax for defining a derived class is:
class ChildClass(ParentClass): # Child class attributes and methods
Here, ChildClass
is the name of the new class, and ParentClass
is the name of the existing class that it inherits from.
Example of Inheritance in Python
Let’s illustrate the concept of inheritance with an example. Suppose we have a base class called Person
that has two attributes, name
and age
, and two methods, greet()
and walk()
. We want to create a derived class called Employee
that inherits from the Person
class and adds a new attribute salary
and a new method work()
.
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name}.") def walk(self): print(f"{self.name} is walking.") class Employee(Person): def __init__(self, name, age, salary): super().__init__(name, age) self.salary = salary def work(self): print(f"{self.name} is working.")
In the above example, Employee
is a derived class that inherits from the Person
class. It adds a new attribute salary
and a new method work()
. We can see that the Employee
class has access to the attributes and methods of the Person
class, which it inherits by default. It can also add new attributes and methods, as we see with the salary
and work()
attributes.
Let’s create an object of the Employee
class and call its methods.
emp = Employee("John", 30, 5000) emp.greet() # Output: Hello, my name is John. emp.walk() # Output: John is walking. emp.work() # Output: John is working. print(emp.salary) # Output: 5000
Here, we created an object emp
of the Employee
class and called its methods greet()
, walk()
, and work()
. We also accessed the salary
attribute of the Employee
object. Since Employee
is a derived class, it can access the methods and attributes of the Person
class using the dot notation.
Conclusion
Inheritance is a powerful feature of Python’s object-oriented programming that allows developers to reuse code and build upon existing functionality. By inheriting the attributes and methods of a parent class, a derived class can save time and effort, reduce code duplication, and improve code readability and maintainability. In this blog, we discussed Python Inheritance in detail and provided an example code snippet. By understanding and using inheritance, developers can write better Python code