24 Oct 2023
There are several types of inheritance in OOP:
-
Single Inheritance: In single inheritance, a class inherits from only one base class. This is the simplest form of inheritance, where a subclass derives its properties and behaviors from a single parent. Example of single inheritance, featuring a
Vehiclebase class and aCarsubclass:- Base class:
Vehicle - Subclass:
Car - Description: The
Carclass inherits properties and behaviors from theVehicleclass, such as starting the engine.
Base Class:
Vehicle- Description: The
Vehicleclass represents a generic vehicle and includes attributes and methods common to all vehicles.
+-------+ | Vehicle | +-------+ | color | | wheels | | start_engine() | | stop_engine() | +-------+Subclass:
Car- Description: The
Carclass is a specific type of vehicle and inherits attributes and methods from theVehicleclass. It also includes additional features specific to cars.
+-----+ | Car | +-----+ | model | | fuel_type | | drive() | +-----+In this example:
-
The
Vehicleclass provides basic attributes such ascolorandwheels, along with methods for starting and stopping the engine. -
The
Carclass inherits these attributes and methods from theVehicleclass and adds specific attributes such asmodelandfuel_type. It also includes a methoddrive()for car-specific functionality.
This demonstrates single inheritance, where the
Carclass derives from theVehicleclass, inheriting and extending its attributes and methods to create a more specialized class for cars. - Base class:
-
Multiple Inheritance: Multiple inheritance allows a class to inherit attributes and behaviors from more than one base class.
Example of multiple inheritance for classes representing animals, dogs, and puppies:
Base Classes:
Animal: Represents generic attributes and behaviors of animals.Dog: Represents attributes and behaviors specific to dogs.
Subclass:
Puppy- Description: The
Puppyclass is a subclass that inherits from bothAnimalandDog. This subclass combines the characteristics of generic animals and dogs to represent young dogs, or puppies.
+-------+ | Animal| +-------+ | eat() | | sleep()| +-------+ | | v +----+ | Dog| +----+ | bark()| | wag_tail()| +----+ | | v +--------+ | Puppy | +--------+ | eat() | (Inherited from Animal) | sleep()| (Inherited from Animal) | bark() | (Inherited from Dog) | wag_tail()| (Inherited from Dog) | play() | (Puppy-specific method) +--------+In this example, the
Puppyclass inherits attributes and methods from bothAnimalandDog. It can perform generic animal actions like eating and sleeping (inherited fromAnimal) and dog-specific actions like barking and wagging its tail (inherited fromDog). Additionally, it has a specific method,play(), which is unique to puppies. This demonstrates multiple inheritance in the context of animal, dog, and puppy classes. -
Multilevel Inheritance: Multilevel inheritance involves a chain of inheritance with multiple levels of classes. In this type of inheritance, a subclass inherits from a base class, and then another class inherits from this subclass. This forms a hierarchy of classes.
Example of multilevel inheritance hierarchy:
+---------+ | Animal | +---------+ | | | eat() | | move() | +---------+ | | v +-------+ | Mammal| +-------+ | | | walk()| | sound()| +-------+ | | v +------+ | Whale| +------+ | | | swim()| | sing()| +------+In this modified example:
- The
Animalclass defines basic methods likeeat()andmove()that all animals share. - The
Mammalclass inherits fromAnimaland adds specific methods for mammals, such aswalk()andsound(). - The
Whaleclass inherits fromMammaland adds features specific to whales, such asswimandsing.
This hierarchy still demonstrates multilevel inheritance, where each subclass builds upon the characteristics and behaviors of the class it inherits from, creating a more specialized class.
- The
-
Hierarchical Inheritance: In hierarchical inheritance, multiple classes inherit from a single base class. This means that multiple subclasses share a common parent class, allowing them to inherit its attributes and methods.
Example of hierarchical inheritance with the
Shape,Circle,Square, andTriangleclasses:Base Class:
Shape- Description: The
Shapeclass represents a general category of shapes and includes methods to calculate the area and perimeter, although they may not have specific implementations since the calculation method depends on the shape.
Subclasses:
Circle,Square, andTriangle- Description: Each of these subclasses inherits from the
Shapeclass. They share common properties and methods from theShapeclass, but they also provide specific implementations of area and perimeter calculations for their respective shapes.
+-------+ | Shape | +-------+ | area()| | perimeter()| +-------+ | | v +--------+ | Circle | +--------+ | radius | | area() | | perimeter() | +--------+ | | v +--------+ | Square | +--------+ | side | | area() | | perimeter() | +--------+ | | v +---------+ | Triangle | +---------+ | base | | height | | area() | | perimeter() | +---------+In this example,
Circle,Square, andTriangleare subclasses ofShape. They inherit the commonarea()andperimeter()methods from theShapeclass. However, each of them also has its specific attributes and methods for calculating the area and perimeter based on their respective shapes.This hierarchical inheritance structure represents a hierarchy of shapes, where each shape type inherits from the common
Shapeclass, showcasing the relationship between these classes. - Description: The
-
Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance mentioned above. It can include single, multiple, multilevel, or hierarchical inheritance within a single program.
Example of hybrid inheritance involving the classes
Employee,Student,Freelancer, andWorkingStudent:Base Classes:
Employee: Represents an individual who is employed in a company and has attributes and methods related to employment, such as salary calculation.Student: Represents an individual who is pursuing education and has attributes and methods related to academic performance, such as grades.Freelancer: Represents an individual who works on a freelance basis and has attributes and methods related to freelance work, such as project assignments.
Subclass:
WorkingStudent- Description: The
WorkingStudentclass is a subclass that inherits from bothEmployeeandStudent. This subclass represents an individual who is both employed and pursuing education. Additionally,WorkingStudentmay inherit specific attributes or methods fromFreelancer, illustrating hybrid inheritance.
+-----------+ | Employee | +-----------+ | salary() | +-----------+ | | v +--------+ | Student| +--------+ | grades | +--------+ | | v +-----------+ | Freelancer| +-----------+ | projects | +-----------+ | | v +---------------+ | WorkingStudent| +---------------+ | salary() | | grades | | projects | +---------------+In this example, the
WorkingStudentclass inherits both employment-related attributes and methods fromEmployeeand academic attributes and methods fromStudent. It may also inherit attributes and methods related to freelance work fromFreelancer. This showcases hybrid inheritance, asWorkingStudentcombines features from multiple base classes to represent an individual who is employed, pursuing education, and possibly freelancing.