Relationships – Inheritance: purpose and its types

Comprehensive study notes, diagrams, and exam preparation for Relationships – Inheritance: purpose and its types.

Relationships – Inheritance: purpose and its types

Definition

Inheritance is the mechanism by which one class, called the derived class or subclass, acquires the properties and behaviors of another class, called the base class or superclass.

In many programming languages, inheritance is used to:

  • reuse existing code,
  • add new features to an existing class,
  • and create hierarchical relationships among classes.

Example:

  • Vehicle is a base class.
  • Car and Bike can inherit from Vehicle.
  • Both Car and Bike automatically receive shared features like speed, start(), and stop().

Main Content

1. Purpose of Inheritance

Code Reusability

Inheritance allows programmers to use existing class features again instead of rewriting them. For example, if a Person class already contains name, age, and displayInfo(), then a Student class can inherit these features and add only student-specific details like rollNumber and course.

Extensibility and Specialization

A derived class can extend the functionality of a base class by adding new data members and methods. This is useful when a general concept needs to be refined into more specific forms. For example, a Shape class may be extended into Circle, Rectangle, and Triangle, each with its own area() method.

Logical Organization of Code

Inheritance helps structure programs in a natural hierarchy. Common features go into a general class, while specific features remain in specialized classes. This makes large systems easier to understand, test, and maintain.

Supports Polymorphism

Inheritance often works together with polymorphism. A function can accept a base-class object reference or pointer and work with derived-class objects, enabling flexible and dynamic behavior.

Reduced Maintenance Effort

When shared behavior changes, it can often be modified in the base class once, and the change automatically benefits all derived classes. This lowers duplication and reduces the chance of inconsistent code.

2. Types of Inheritance

Single Inheritance

One derived class inherits from one base class.
Example: Student inherits from Person.

  Person
    |
  Student

Multilevel Inheritance

A class is derived from another derived class, creating a chain.
Example: LivingBeing -> Animal -> Dog

  LivingBeing
      |
    Animal
      |
      Dog

Hierarchical Inheritance

Multiple derived classes inherit from the same base class.
Example: Car, Bike, and Truck all inherit from Vehicle.

      Vehicle
     /   |   \
   Car  Bike Truck

Multiple Inheritance

One derived class inherits from more than one base class.
Example: SmartPhone may inherit features from Phone and Camera.

   Phone    Camera
      \      /
      SmartPhone

Hybrid Inheritance

A combination of two or more types of inheritance. It may include single, multiple, multilevel, or hierarchical inheritance together.
Example: A system may have a mix of multilevel and multiple inheritance patterns.

3. Key Terms and Concepts Related to Inheritance

Base Class / Superclass

The parent class whose features are inherited by another class. It contains common properties and methods.

Derived Class / Subclass

The child class that inherits from the base class and may add new features or modify inherited ones.

Method Overriding

When a derived class provides its own version of a method already defined in the base class. This is common when a child class needs behavior different from the parent class.

Access Control in Inheritance

Not all members of a base class may be directly accessible in the derived class, depending on the language and access modifier used (public, protected, private). In many languages, private members are inherited but not directly accessible.

“Is-A” Relationship

Inheritance represents an “is-a” relationship. For example, a Car is a Vehicle, so inheritance makes sense. If the relationship is not “is-a,” inheritance may not be the correct design choice.


Working / Process

1. Identify the General Class

  • Determine the common features shared by a group of objects.
  • Create a base class for these shared attributes and methods.
  • Example: Create a Vehicle class with brand, speed, start(), and stop().

2. Create the Specialized Class

  • Define a new class that represents a more specific version of the base class.
  • The specialized class inherits the common features automatically.
  • Example: Create a Car class that inherits from Vehicle.

3. Extend or Modify Behavior

  • Add new fields and methods that are unique to the derived class.
  • Override inherited methods if the child class needs different behavior.
  • Example: Car may add numberOfDoors and override start() to include car-specific startup logic.

4. Use the Derived Class in Programs

  • Instantiate the derived class and access inherited as well as new members.
  • This allows cleaner code with less repetition.
  • Example: A Car object can use brand from Vehicle and numberOfDoors from Car.

5. Apply Polymorphic Behavior if Needed

  • Use base-class references or interfaces to handle derived objects in a general way.
  • This allows one piece of code to work with many related object types.
  • Example: A Vehicle reference can refer to a Car, Bike, or Truck object.

Advantages / Applications

Promotes Reusability

Common code is written once and reused in many related classes, which saves time and effort.

Improves Readability and Structure

Class hierarchies make it easier to understand relationships among objects and manage large programs.

Simplifies Maintenance and Updates

Fixing or improving shared behavior in one base class can affect all derived classes consistently.

Supports Real-World Modeling

Inheritance is ideal for representing natural hierarchies such as Employee -> Manager, Animal -> Dog, or Account -> SavingsAccount.

Enables Flexible Design

It supports extension, specialization, and polymorphism, making systems easier to expand without major rewrites.


Summary

Inheritance lets one class acquire and extend the features of another class, helping organize code into clear parent-child relationships and reducing repetition.

  • Inheritance connects a base class and a derived class.
  • It is used to reuse and extend existing behavior.
  • Common types include single, multilevel, hierarchical, multiple, and hybrid inheritance.
  • Important terms to remember: base class, derived class, method overriding, and “is-a” relationship.