KTU OOP Java2024 Scheme • 60M ESE
MODULE 2/Section 1: Polymorphism

Polymorphism & Method Overloading (Compile-Time Binding & Resolution Rules)

HIGH PRIORITY (Prep Guide)Official Syllabus~15 min study
Official KTU 2024 Syllabus Topics Covered Here
Meaning of polymorphismCompile-time and runtime polymorphismMethod overloadingRules of method overloading

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.

2. Start From Zero: One Word ('Charge'), Multiple Meanings by Context

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.

Key Takeaway: Polymorphism has two forms: Compile-time (Static binding via Method Overloading) and Runtime (Dynamic binding via Method Overriding). Overloading MUST differ in parameter count, types, or order—return type alone is NEVER enough!

3. Essential Exam Definitions

Polymorphism
OOP ability of an interface or method name to take multiple forms ('one interface, multiple methods'), resolved either statically at compile time or dynamically at runtime.
Compile-Time Polymorphism (Static Binding)
Polymorphism resolved entirely by javac during compilation based on method signatures and argument types (e.g., Method Overloading and Constructor Overloading).
Method Overloading
Defining two or more methods within the same class (or inherited hierarchy) sharing the same name but possessing different parameter lists (number, type, or sequence of parameters).

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) vs add(int, int, int)); (2) Different parameter types (abs(int) vs abs(double)); (3) Different sequence of distinct types (show(int, String) vs show(String, int)).
  • INVALID Overload (Return Type Rule): Changing ONLY the return type (int sum(int a) vs double 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 int to test(double a) when test(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. 1. Look for an exact type match for every argument.
  2. 2. If no exact match exists, try widening primitive types (byte -> short -> int -> long -> float -> double, char -> int).
  3. 3. If still unmatched, consider autoboxing (int -> Integer) or varargs (int...) as final fallback.
  4. 4. If two widening/overload paths are equally specific (e.g., sum(int, double) vs sum(double, int) called with sum(5, 10)), javac rejects the call as AMBIGUOUS.

7. Complete Java Code Examples & Tracing

Method Overloading with Exact Match & Automatic Type ConversionOverloadDemo.java
Demonstrates overloaded test() methods and shows how javac widens int to double when no test(int) version exists.
1public 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}
Expected Output
No parameters
Two ints: 10 and 20
Inside test(double): 88.0
Inside test(double): 123.25
Score: 59
Keywords:voiddoubleint
Common Exam & Coding Mistakes
  • Attempting to overload solely by changing return type (int f(int x) and double f(int x) -> compile error).
  • Creating ambiguous overloads like calc(int a, double b) and calc(double a, int b) and calling calc(5, 10).

Comparison Tables

Compile-Time Polymorphism vs Runtime Polymorphism
FeatureCompile-Time PolymorphismRuntime Polymorphism
Also Known AsStatic Binding / Early BindingDynamic Binding / Late Binding
MechanismMethod / Constructor OverloadingMethod Overriding + Dynamic Method Dispatch
Resolved ByJava Compiler (javac) at compile timeJVM at runtime based on actual Heap object
ScopeUsually within the same classAcross superclass & subclass hierarchy (extends)
Method SignatureSame name, DIFFERENT parameter listIDENTICAL name and parameter signature

Real-World Applications

  • java.lang.Math.abs() overloads int, long, float, and double.
  • 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 with int argument).

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) and void 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?