codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Compile-Time Polymorphism (Overloading)

Updated 2026-05-06
2 min read

Compile-Time Polymorphism (Overloading)

Polymorphism (from the Greek words poly meaning "many" and morph meaning "forms") is the ability of a single entity (method, operator, or object) to take on multiple forms. It is the fourth pillar of OOP.

There are two fundamental types of polymorphism:

  1. Compile-Time Polymorphism (Static Binding): The decision of which method to call is made by the compiler at compile time. Achieved via Method Overloading and Operator Overloading.
  2. Run-Time Polymorphism (Dynamic Binding): The decision is made at runtime. Achieved via Method Overriding (covered in the next chapter).

1. Method Overloading

Method Overloading allows a class to have multiple methods with the same name but with different parameter lists (different number of parameters, different types of parameters, or both).

The compiler determines which version of the overloaded method to call based on the arguments provided at the call site. This decision is resolved entirely at compile time.

class Calculator {
    // Version 1: Two integers
    int add(int a, int b) {
        return a + b;
    }

    // Version 2: Three integers
    int add(int a, int b, int c) {
        return a + b + c;
    }

    // Version 3: Two doubles
    double add(double a, double b) {
        return a + b;
    }
}

Calculator calc = new Calculator();
calc.add(2, 3);       // Calls Version 1 -> returns 5
calc.add(2, 3, 4);    // Calls Version 2 -> returns 9
calc.add(2.5, 3.5);   // Calls Version 3 -> returns 6.0

Rules for Valid Overloading:

  1. Methods must have the same name.
  2. Methods must have a different parameter list (number, type, or order of parameters).
  3. Return type alone is NOT sufficient to overload a method. int add(int a) and double add(int a) will cause a compile error because the compiler cannot distinguish which one to call from calc.add(5).

2. Constructor Overloading

As we saw in the Constructors chapter, constructors can also be overloaded. This is a very common use case of compile-time polymorphism.

class Student {
    Student() { /* default */ }
    Student(String name) { /* with name */ }
    Student(String name, int age) { /* with name and age */ }
}

3. Operator Overloading

Operator Overloading allows you to redefine the behavior of standard operators (+, -, *, ==) for user-defined classes.

  • C++ supports operator overloading extensively.
  • Java does NOT support operator overloading (except for the + operator which is pre-overloaded for String concatenation).
// C++ Operator Overloading Example
class Complex {
    double real, imag;
public:
    Complex operator+(const Complex& other) {
        Complex result;
        result.real = this->real + other.real;
        result.imag = this->imag + other.imag;
        return result;
    }
};

Complex a, b, c;
c = a + b; // The '+' operator now works on Complex objects!

4. Why is it called "Compile-Time"?

The binding between the method call and the method body happens at compile time because the compiler has all the information it needs: the method name and the argument types. It doesn't need to wait until the program is running to figure out which add() version to use. This makes overloaded method calls extremely fast, with zero runtime overhead.



PreviousInheritance Types (Single, Multiple)NextPolymorphism & Interfaces

Recommended Gear

Inheritance Types (Single, Multiple)Polymorphism & Interfaces