Lesson 3
Inheritance in Python
Welcome to Inheritance

Welcome back! Now that you have a solid understanding of classes and objects in Python, it's time to build on that knowledge by exploring inheritance. Consider it a natural progression in our journey into object-oriented programming (OOP).

Inheritance allows you to create a new class based on an existing class. By using inheritance, you can reuse code, add new features, and make your programs easier to manage and understand. Let's dive in and see what it's all about.

Introduction to Inheritance

Inheritance is a way to establish a relationship between a new class (derived class) and an existing class (base class). The derived class inherits properties and behaviors (methods) from the base class.

Here’s a simple example:

Python
1class Person: 2 def __init__(self, name, age): 3 self.name = name 4 self.age = age 5 6 def display(self): 7 print(f"Name: {self.name}, Age: {self.age}") 8 9class Student(Person): 10 def __init__(self, name, age, major): 11 super().__init__(name, age) 12 self.major = major 13 14 def display(self): 15 super().display() 16 print(f"Major: {self.major}") 17 18if __name__ == "__main__": 19 student = Student("Bob", 25, "Computer Science") 20 student.display() 21 # Output: 22 # Name: Bob, Age: 25 23 # Major: Computer Science

In this snippet, the Student class inherits from the Person class. It reuses the name and age attributes and the display method from the Person class, and it adds a new attribute, major.

You can think of inheritance like a university system. Imagine a university where all students are automatically considered persons, because they have fundamental attributes such as a name and age just like any other person. However, not every person is a student; for someone to be a student, they need to have additional attributes, such as a major. In the same way, a derived class (like Student) inherits basic attributes and methods from the base class (like Person) but can also have additional attributes and functionality of its own.

How Inheritance Works

When you declare a derived class, you specify the base class it inherits from using the class DerivedClass(BaseClass) syntax. The derived class extends or overrides the functionality of the base class.

In our example, the Person class serves as the base class, while the Student class is the derived class, inheriting name and age attributes from the Person class. The Student class also introduces an additional attribute, major, and overrides the display method to provide information about the major.

Notice how Student.display() calls Person.display() using super() to reuse the base class functionality before adding its own details. By overriding, you have the ability to completely redefine the method's function in the derived class, which means the base class method will not automatically be executed. If you still want the base class's method to be part of the derived class's method execution, you have to explicitly call it using super(). This is crucial for maintaining the fundamental behavior of the base class while introducing new or extended features in the derived class.

Extending Functionality

A derived class can also introduce completely new methods, providing additional functionality:

Python
1class Employee(Person): 2 def __init__(self, name, age, position): 3 super().__init__(name, age) 4 self.position = position 5 6 def display(self): 7 super().display() 8 print(f"Position: {self.position}") 9 10 def work(self): 11 print(f"{self.name} is working as a {self.position}") 12 13if __name__ == "__main__": 14 employee = Employee("Alice", 30, "Engineer") 15 employee.display() 16 employee.work() 17 # Output: 18 # Name: Alice, Age: 30 19 # Position: Engineer 20 # Alice is working as a Engineer

Here, the Employee class extends the Person class by adding a position attribute and a work method. It also overrides the display method to include information about the team size.

By understanding and utilizing inheritance, you can create a class hierarchy that promotes code reusability, extensibility, and a clear structure that mirrors real-world relationships. This approach enables you to design flexible and maintainable object-oriented applications.

Conclusion

Inheritance in Python allows you to build on existing classes and reuse code efficiently. By using inheritance, you can create a class hierarchy that mirrors real-world relationships and provides a clear and maintainable structure for your applications.

Understanding and leveraging inheritance will enable you to design flexible, scalable, and organized object-oriented applications. With inheritance, you can extend existing functionalities, reduce code duplication, and develop more robust and readable programs.

Inheritance is a cornerstone of OOP, and understanding it will enable you to design more flexible and scalable applications. It's an essential concept for mastering OOP.

Excited to start practicing? Let's move on and put this theory into action!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.