Inheritance
ICT2622 - Object-Oriented Analysis · Object-Oriented Concepts
Inheritance
Inheritance is a fundamental concept in object-oriented programming (OOP). It allows a new class, known as a subclass or derived class, to inherit properties and methods from an existing class, known as a superclass or base class. This feature promotes code reusability and establishes a relationship between classes.
Basic Concepts of Inheritance
In OOP, a class is a blueprint for creating objects. A subclass can extend the functionality of a superclass by adding new properties and methods or by overriding existing ones. This relationship can be visualised as a hierarchy where the superclass is at the top and subclasses branch out below.
Remember: Inheritance promotes code reusability and helps in maintaining a clean and manageable code structure.
Types of Inheritance
There are several types of inheritance in OOP:
- Single Inheritance: A subclass inherits from one superclass.
- Multiple Inheritance: A subclass inherits from more than one superclass. (Note: C++ supports multiple inheritance, but it can lead to complexity.)
- Multilevel Inheritance: A subclass is derived from a superclass, which is also a subclass of another superclass.
- Hierarchical Inheritance: Multiple subclasses are derived from a single superclass.
- Hybrid Inheritance: A combination of two or more types of inheritance.
Single Inheritance Example
Consider a simple example of a class hierarchy for vehicles. We will create a base class called Vehicle and a derived class called Car.
class Vehicle { // Superclass public: void start() { cout << "Vehicle started" << endl; } void stop() { cout << "Vehicle stopped" << endl; }};class Car : public Vehicle { // Subclass public: void honk() { cout << "Car honking" << endl; }};In this example, the Car class inherits the start and stop methods from the Vehicle class. You can create an object of Car and use both inherited and its own methods:
int main() { Car myCar; myCar.start(); myCar.honk(); myCar.stop(); return 0;}Multiple Inheritance Example
In C++, you can create a class that inherits from more than one class. For example, consider a HybridCar class that inherits from both Car and ElectricVehicle.
class ElectricVehicle { // Another Superclass public: void charge() { cout << "Charging electric vehicle" << endl; }};class HybridCar : public Car, public ElectricVehicle { // Subclass public: void drive() { cout << "Driving hybrid car" << endl; }};In this case, the HybridCar class inherits methods from both Car and ElectricVehicle. You can create an object of HybridCar and call methods from both superclasses:
int main() { HybridCar myHybrid; myHybrid.start(); myHybrid.charge(); myHybrid.drive(); myHybrid.stop(); return 0;}Watch out: Multiple inheritance can lead to ambiguity if two superclasses have methods with the same name. Always ensure that you manage such cases carefully.
Multilevel Inheritance Example
In multilevel inheritance, a class is derived from another derived class. For example, you can have a Vehicle class, a Car class, and a ElectricCar class that inherits from Car.
class ElectricCar : public Car { // Subclass of Car public: void charge() { cout << "Charging electric car" << endl; }};Here, the ElectricCar class inherits methods from the Car class, which in turn inherits from Vehicle. You can create an object of ElectricCar and use methods from both Car and Vehicle:
int main() { ElectricCar myElectricCar; myElectricCar.start(); myElectricCar.honk(); myElectricCar.charge(); myElectricCar.stop(); return 0;}Hierarchical Inheritance Example
In hierarchical inheritance, multiple subclasses inherit from a single superclass. For example, both Car and Truck can inherit from the Vehicle class.
class Truck : public Vehicle { // Another subclass public: void load() { cout << "Loading truck" << endl; }};In this case, both Car and Truck have access to the methods of the Vehicle class. You can create objects of both subclasses:
int main() { Car myCar; Truck myTruck; myCar.start(); myTruck.start(); myTruck.load(); return 0;}Overriding Methods
A subclass can also override a method from its superclass. This means that the subclass provides its own implementation of a method that is already defined in the superclass. To override a method, the method in the subclass must have the same name, return type, and parameters as the method in the superclass.
class Car : public Vehicle { // Subclass public: void start() { cout << "Car started" << endl; }}In this case, the start method in the Car class overrides the start method from the Vehicle class. If you create an object of Car and call start, it will use the overridden version:
int main() { Car myCar; myCar.start(); return 0;}Tip: Use the virtual keyword in the superclass method declaration if you want to allow overriding in derived classes. This makes the method dynamic.
Abstract Classes and Inheritance
An abstract class is a class that cannot be instantiated on its own and is meant to be a base class for other classes. It can contain pure virtual functions, which are functions that must be overridden in derived classes. You define a pure virtual function by using the syntax virtual void functionName() = 0;.
class Vehicle { // Abstract class public: virtual void start() = 0; // Pure virtual function};Any class that derives from an abstract class must implement the pure virtual functions. For example:
class Car : public Vehicle { // Derived class public: void start() { cout << "Car started" << endl; }}Summary
- Inheritance allows a subclass to inherit properties and methods from a superclass.
- Types of inheritance include single, multiple, multilevel, hierarchical, and hybrid inheritance.
- Subclass methods can override superclass methods.
- Abstract classes cannot be instantiated and may contain pure virtual functions.
Check your understanding
- What is inheritance in object-oriented programming?
- Explain the difference between single and multiple inheritance.
- What happens when a subclass overrides a method from its superclass?
- Define an abstract class and its purpose in inheritance.