Polymorphism & Method Overloading (Compile-Time Binding & Resolution Rules)
1. Topic Introduction
In C, computing the absolute value of an int, long, and double required memorizing three distinct function names (abs(), labs(), fabs()). Java eliminates that clutter through Polymorphism ('many forms'), starting with Compile-Time Method Overloading—letting one clean method name (Math.abs() or area()) handle multiple parameter combinations intelligently.
If someone says 'Charge!' to an electrician, they plug in a cable. If they say 'Charge!' to a cashier, they swipe a card. Same command name, different inputs, automatically resolved by context! In Java Method Overloading, you define multiple methods with the SAME name (printArea) inside one class, each expecting different inputs ((int side) vs (int l, int b) vs (double r)). The compiler checks your arguments BEFORE running the program and picks the exact match.
3. Essential Exam Definitions
javac during compilation based on method signatures and argument types (e.g., Method Overloading and Constructor Overloading).4. Detailed Technical Explanation & Syntax
Rules of Method Overloading & Overload Resolution
Java uses the number, type, and order of arguments at the call site to pick which overloaded version to execute:
- Valid Overload Changes: (1) Different number of parameters (
add(int, int)vsadd(int, int, int)); (2) Different parameter types (abs(int)vsabs(double)); (3) Different sequence of distinct types (show(int, String)vsshow(String, int)). - INVALID Overload (Return Type Rule): Changing ONLY the return type (
int sum(int a)vsdouble sum(int a)) causes a COMPILE ERROR (method already defined) because the caller can ignore the return value (sum(5);), making resolution impossible! - Automatic Type Widening in Resolution: Java always looks for an exact parameter match first. If none exists, it uses implicit widening conversions (e.g., passing
inttotest(double a)whentest(int)is absent). - Design Discipline: Never overload unrelated actions under the same name (e.g., using
sqr()for integer square AND floating-point square root)—keep overloaded methods semantically consistent.
Compile-Time vs Runtime Polymorphism Overview
Compile-time polymorphism (Method Overloading) decides the target method during javac compilation, offering zero runtime lookup cost. Runtime polymorphism (Method Overriding + Dynamic Method Dispatch, covered in Section 2) waits until runtime to inspect the actual Heap object behind a superclass reference.
5. Working Principle: How javac Resolves an Overloaded Method Call
- 1. Look for an exact type match for every argument.
- 2. If no exact match exists, try widening primitive types (
byte -> short -> int -> long -> float -> double,char -> int). - 3. If still unmatched, consider autoboxing (
int -> Integer) or varargs (int...) as final fallback. - 4. If two widening/overload paths are equally specific (e.g.,
sum(int, double)vssum(double, int)called withsum(5, 10)),javacrejects the call as AMBIGUOUS.
7. Complete Java Code Examples & Tracing
test() methods and shows how javac widens int to double when no test(int) version exists.| 1 | public class OverloadDemo { |
| 2 | void test() { |
| 3 | System.out.println("No parameters"); |
| 4 | } |
| 5 | |
| 6 | // Overloaded for two integers |
| 7 | void test(int a, int b) { |
| 8 | System.out.println("Two ints: " + a + " and " + b); |
| 9 | } |
| 10 | |
| 11 | // Overloaded for one double (note: NO test(int) exists!) |
| 12 | void test(double a) { |
| 13 | System.out.println("Inside test(double): " + a); |
| 14 | } |
| 15 | |
| 16 | // Overloaded by parameter order |
| 17 | void test(String label, int val) { |
| 18 | System.out.println(label + ": " + val); |
| 19 | } |
| 20 | |
| 21 | public static void main(String[] args) { |
| 22 | OverloadDemo ob = new OverloadDemo(); |
| 23 | int i = 88; |
| 24 | |
| 25 | ob.test(); |
| 26 | ob.test(10, 20); |
| 27 | ob.test(i); // Automatic widening: int 88 -> double 88.0 |
| 28 | ob.test(123.25); // Exact match: test(double) |
| 29 | ob.test("Score", 59); |
| 30 | } |
| 31 | } |
No parameters Two ints: 10 and 20 Inside test(double): 88.0 Inside test(double): 123.25 Score: 59
voiddoubleint- Attempting to overload solely by changing return type (
int f(int x)anddouble f(int x)-> compile error). - Creating ambiguous overloads like
calc(int a, double b)andcalc(double a, int b)and callingcalc(5, 10).
Comparison Tables
| Feature | Compile-Time Polymorphism | Runtime Polymorphism |
|---|---|---|
| Also Known As | Static Binding / Early Binding | Dynamic Binding / Late Binding |
| Mechanism | Method / Constructor Overloading | Method Overriding + Dynamic Method Dispatch |
| Resolved By | Java Compiler (javac) at compile time | JVM at runtime based on actual Heap object |
| Scope | Usually within the same class | Across superclass & subclass hierarchy (extends) |
| Method Signature | Same name, DIFFERENT parameter list | IDENTICAL name and parameter signature |
Real-World Applications
java.lang.Math.abs()overloadsint,long,float, anddouble.PrintStream.println()is overloaded 10 times (println(int),println(String),println(boolean),println(Object), etc.).
Important KTU Exam Points
- Always state: 'Return types play NO role in overload resolution' (classic 3-mark KTU rule!).
- Show automatic type conversion in overloading (
test(double)invoked withintargument).
Memory Techniques & Revision
- Overload = Same Name + Different Parameters ('N-T-O': Number, Type, Order). Return type CANNOT differentiate!
Common Pitfalls to Avoid
- Writing two methods differing only by parameter variable names (
void m(int a)andvoid m(int b))—names don't change signature!
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: Method overloading is Java's compile-time polymorphism mechanism where multiple methods in a class share the same name while differing in parameter declarations.
- Rules: Overloaded methods must differ in (1) number of parameters, (2) data types of parameters, or (3) sequence/order of distinct parameter types.
- Return Type Rule: Return types play NO role in overload resolution. Two methods with identical parameters and different return types fail compilation.
- Type Promotion Rule: Java searches for an exact match first; if absent, automatic widening conversions (
int -> double) are applied.
| class Calc { |
| int add(int a, int b) { return a + b; } |
| double add(double a, double b) { return a + b; } |
| } |
Quick Understanding Check
1. Which pair of method declarations causes a COMPILE ERROR in the same class?