Inheritance and Polymorphism

Inheritance lets a subclass reuse and extend a superclass (an IS-A relationship), and polymorphism lets a base-type reference invoke the correct overridden method of the actual object at runtime through dynamic dispatch.

Key formulas & points

Skim these first — then read the full notes below.

  • Single versus multiple inheritance differs between Java and C++
  • Prefer composition over inheritance for flexibility
  • Liskov substitution: a subclass must honour the base contract

Topic details

Introduction

This topic covers code reuse and runtime flexibility. You build class hierarchies, override methods, understand dynamic dispatch via the virtual table, distinguish overriding from overloading, and apply the Liskov substitution principle to keep hierarchies sound.

Key relations & formulas

Formulas (Indian textbook notation)

  • subclassISAsuperclass(extendsandspecialisesit)subclass IS-A superclass (extends and specialises it)

Formulas (Indian textbook notation)

  • overriding:samesignature,resolvedatruntimeviathevtableoverriding: same signature, resolved at runtime via the vtable

Formulas (Indian textbook notation)

  • upcast:abasereferencetoasubclassobjectcallstheoverriddenmethodupcast: a base reference to a subclass object calls the overridden method

Notation and sign conventions

Relation 1 —
subclassISAsuperclasssubclass IS-A superclass

Formulas (Indian textbook notation)

  • subclassISAsuperclass(extendsandspecialisesit)subclass IS-A superclass (extends and specialises it)
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
overriding:samesignature,resolvedatruntimeviathevtableoverriding: same signature, resolved at runtime via the vtable

Formulas (Indian textbook notation)

  • overriding:samesignature,resolvedatruntimeviathevtableoverriding: same signature, resolved at runtime via the vtable
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
upcast:abasereferencetoasubclassobjectcallstheoverriddenmethodupcast: a base reference to a subclass object calls the overridden method

Formulas (Indian textbook notation)

  • upcast:abasereferencetoasubclassobjectcallstheoverriddenmethodupcast: a base reference to a subclass object calls the overridden method
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Inheritance expresses specialisation: a subclass inherits the base’s members and can add or override behaviour, modelling an IS-A relationship. Polymorphism is what makes this powerful — a variable of the base type can hold any subclass object, and a virtual method call is dispatched at runtime to the object’s actual type through a vtable, so client code written against the base works with every subclass. This only stays correct if subclasses honour the base’s contract (Liskov substitution); a subclass that weakens guarantees breaks callers. Because deep inheritance is rigid, composition — building behaviour by holding other objects — is often preferred for flexibility.

Assumptions and validity limits

State assumptions explicitly before using any relation for inheritance and polymorphism — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In OOP viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in OOP papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to inheritance and polymorphism.
4. Use equation 1:
subclassISAsuperclasssubclass IS-A superclass
.
5. Use equation 2:
overriding:samesignature,resolvedatruntimeviathevtableoverriding: same signature, resolved at runtime via the vtable
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Inheritance and Polymorphism appears in Java/C++ application development. In Indian it software curricula this topic is tested because it connects theory to classes, inheritance, and polymorphism.
GATE and semester exams often combine inheritance and polymorphism with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use inheritance and polymorphism?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students confuse overriding (same signature, runtime dispatch) with overloading (same name, different parameters, compile-time), and violate Liskov substitution with subclasses that break the base contract. Expecting static/private methods to be polymorphic is another error.

Quick revision checklist

Before attempting inheritance and polymorphism problems, confirm you can:
1. Single versus multiple inheritance differs between Java and C++
2. Prefer composition over inheritance for flexibility
3. Liskov substitution: a subclass must honour the base contract
Revise the solved examples in Programming in ANSI C / OOP with C++ — E. Balagurusamy and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Dynamic dispatch

Problem

Base class Shape has virtual method area(); Circle overrides it. A Shape reference points to a Circle. Which area() runs?

Solution

Circle’s area() runs. With a virtual method, the call is dispatched at runtime by the object’s actual type (Circle), not by the reference type (Shape).

Conceptual check — Inheritance and Polymorphism

Problem

In a OOP semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of inheritance and polymorphism." What should a complete answer include?

📖 Standard books (India)

  • Programming in ANSI C / OOP with C++E. Balagurusamy

    Read: Syllabus unit

    Standard Indian classroom programming text