Skip to content

Chapter 5 of 8

Polymorphism and Method Dispatch

Polymorphism in object-oriented programming is enabled by two distinct mechanisms known as overloading and overriding. Method overloading, the form of compile-time polymorphism, occurs when multiple methods share the same name but differ in their parameter lists within the same class. For instance, a class might define both an add method accepting two integers and another accepting two doubles, and the compiler selects the correct version based on the argument types at the call site.

Method overriding is the run-time counterpart: a subclass provides a specific implementation of a method already declared in its superclass. When a Dog class extends Animal and overrides speak to print "Woof", the method called depends on the actual object type at runtime, not the declared reference type. This is what enables a single method call to produce different behavior depending on the underlying object.

The distinction between overloading and overriding runs deeper than their names suggest. Overloading involves compile-time binding within the same class with different parameters and possibly different return types, while overriding involves runtime binding across parent and child classes with identical signatures and return types that must be the same or covariant. Overloading is therefore static polymorphism, and overriding is dynamic polymorphism.

Underlying overriding is the concept of virtual methods. In C++, a method declared with the virtual keyword can be overridden by subclasses, with the call resolved at runtime through dynamic dispatch. In Java, all non-static, non-final methods are virtual by default, while in C# the virtual keyword must be used explicitly. A pure virtual function in C++, written as virtual void draw() = 0, has no implementation and forces subclasses to provide one, and a class containing such a function becomes abstract. These mechanisms together form the foundation of late binding, where the system checks the actual object type rather than the declared type when dispatching a method call, in contrast to early binding used for overloaded, static, and final methods. Virtual methods are also key to design patterns such as the Template Method.

All chapters
  1. 1The Four Pillars of OOP
  2. 2The SOLID Design Principles
  3. 3Classes, Objects, and Language Mechanics
  4. 4Object Relationships
  5. 5Polymorphism and Method Dispatch
  6. 6Abstraction Through Interfaces and Abstract Classes
  7. 7Composition, Inheritance, and Code Reuse
  8. 8General Software Design Principles

Drill it

Reading is not remembering. These come from the Oop Principles deck:

Q

What is encapsulation in OOP?

Encapsulation is the bundling of data and methods that operate on that data into a single unit (class), while restricting direct access to some components.Key b...

Q

What is inheritance in OOP?

Inheritance allows a child class (subclass) to acquire the properties and behaviors of a parent class (superclass).Example in Java:class Dog extends Animal { }B...

Q

What is polymorphism in OOP?

Polymorphism means "many forms" — the ability of an object to take on different behaviors depending on its type.Two main types:Compile-time (static): method ove...

Q

What is abstraction in OOP?

Abstraction is the process of hiding complex implementation details and exposing only the essential features of an object.Achieved through:Abstract classes — pa...