Chapter 5: Inheritance in Object Oriented Programming

Chapter 5: Inheritance

Abstract:
ture of object-oriented programming (OOP) that allows a class to inherit properties and behaviors from another class. 
How does inheritance work?
  • The class that is being inherited from is called the base class or superclass. 
  • The class that inherits from another class is called the derived class, subclass, or child class. 
  • The derived class can add new features or override existing ones. 
Benefits of inheritance
  • Code reuse: A subclass can inherit attributes and methods from its superclass, which minimizes redundancy. 
  • Hierarchy establishment: Inheritance establishes a clear relationship between a subclass and a superclass. 
  • Simplifies programming: You don't have to write the same code again for the new class. 
Types of inheritance Single inheritance, Multiple inheritance, Multilevel inheritance, Hierarchical inheritance, and Hybrid inheritance. 
Example 
  • For example, you could create a parent class called "Animal" and include common properties like name, color, and age. Then, you could create subclasses, such as "Dog," "Cat," "Horse," and so on, to inherit these properties.

So, let's explore the Chapter 5 in detail 

5.1 Introduction to Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class to be created based on an existing class. The new class, known as the child class or subclass, inherits attributes and methods from the parent class or superclass. This promotes code reusability, improves maintainability, and enables hierarchical classification.

By using inheritance, a subclass can extend or modify the functionality of a parent class while maintaining a logical relationship between different objects.

Key Benefits of Inheritance

  • Code Reusability: Reduces redundancy by reusing existing class properties and methods.
  • Modularity: Improves code organization and readability.
  • Extensibility: Allows easy addition of new features without modifying existing code.
  • Polymorphism: Enables method overriding and dynamic behavior.

5.2 Parent-Child Relationship in Inheritance

In an inheritance hierarchy, the parent class provides the basic structure, while the child class extends or customizes it.

Example of Basic Inheritance

class Animal:  # Parent Class
    def speak(self):
        return "Animal makes a sound"

class Dog(Animal):  # Child Class
    def speak(self):
        return "Dog barks"

# Creating objects
a = Animal()
d = Dog()

print(a.speak())  # Output: Animal makes a sound
print(d.speak())  # Output: Dog barks

In this example, Dog inherits from Animal, but it overrides the speak method to provide a specific implementation.


5.3 Types of Inheritance

5.3.1 Single Inheritance

In single inheritance, a child class inherits from only one parent class.

Example of Single Inheritance

class Vehicle:
    def description(self):
        return "This is a vehicle"

class Car(Vehicle):
    def wheels(self):
        return "A car has 4 wheels"

c = Car()
print(c.description())  # Output: This is a vehicle
print(c.wheels())       # Output: A car has 4 wheels

Here, Car inherits from Vehicle, acquiring its properties and methods.


5.3.2 Multiple Inheritance

In multiple inheritance, a child class inherits from more than one parent class. This allows the child class to combine functionalities from multiple sources.

Example of Multiple Inheritance

class Engine:
    def engine_type(self):
        return "Petrol engine"

class Wheels:
    def wheel_count(self):
        return "Has 4 wheels"

class Car(Engine, Wheels):
    def description(self):
        return "This is a car"

c = Car()
print(c.description())  # Output: This is a car
print(c.engine_type())  # Output: Petrol engine
print(c.wheel_count())  # Output: Has 4 wheels

Here, Car inherits from both Engine and Wheels, acquiring attributes from both classes.


5.3.3 Hierarchical Inheritance

In hierarchical inheritance, multiple child classes inherit from a single parent class.

Example of Hierarchical Inheritance

class Animal:
    def speak(self):
        return "Some generic sound"

class Dog(Animal):
    def speak(self):
        return "Dog barks"

class Cat(Animal):
    def speak(self):
        return "Cat meows"

d = Dog()
c = Cat()

print(d.speak())  # Output: Dog barks
print(c.speak())  # Output: Cat meows

Here, Dog and Cat both inherit from Animal and provide their own implementation of speak.


5.4 Overriding Methods

Method overriding allows a child class to provide a different implementation for a method inherited from the parent class.

Example of Method Overriding

class Parent:
    def show(self):
        return "This is the parent class"

class Child(Parent):
    def show(self):
        return "This is the child class"

c = Child()
print(c.show())  # Output: This is the child class

The show method in Child overrides the show method in Parent.

Using super() to Call Parent Method

The super() function allows a child class to call a method from the parent class.

Example of Using super()

class Parent:
    def show(self):
        return "This is the parent class"

class Child(Parent):
    def show(self):
        return super().show() + " (Overridden by child)"

c = Child()
print(c.show())  # Output: This is the parent class (Overridden by child)

Here, super().show() calls the parent class method before adding additional behavior.


5.5 Summary

  • Inheritance enables a child class to reuse and extend the properties and methods of a parent class.
  • Single Inheritance involves one parent and one child class.
  • Multiple Inheritance allows a child class to inherit from multiple parent classes.
  • Hierarchical Inheritance has multiple child classes inheriting from a single parent.
  • Method Overriding lets child classes modify inherited methods.
  • super() allows access to the parent class’s methods.

By understanding and implementing inheritance correctly, developers can write more efficient, modular, and scalable code.

Comments