Back

Activity 3.4 Method Overloading

divider

Introduction

Method Overloading

Same name, different parameters

What Is Method Overloading?

Method overloading allows multiple methods to share the same name as long as their parameter lists are different.

This makes your code cleaner and more flexible — similar tasks can be grouped under one method name.

Built-in Example

The System.out.println() method you've used many times is actually overloaded to handle different data types.

System.out.println(42);
System.out.println(3.14);
System.out.println("Hello world!");
System.out.println(true);

Each call uses a different version of println() — the compiler picks the correct one based on the arguments.

Overloading Rules

  • Parameter list must differ (number, type, or order).
  • Return type alone cannot overload a method.
  • All overloaded versions exist in the same class.

Why Use Overloading?

  • Clarity: Use one logical method name for similar operations.
  • Readability: Easier to understand code structure.
  • Reusability: Extend functionality without rewriting code.

Compile-Time Polymorphism

Overloading is a type of compile-time polymorphism — the compiler determines which version to call based on the arguments.

Key Terms

Method Overloading
Defining multiple methods with the same name but different parameter lists (number, type, or order).
Parameter List
The specific combination of parameter types and order that makes each overloaded method unique.
Method Signature
The method name combined with its parameter list; used by the compiler to distinguish overloaded methods.

Key Terms

Compile-Time Polymorphism
A form of polymorphism where the compiler determines which overloaded method to execute based on the arguments passed.

'F' → Fullscreen

Objectives

  • icon Define and use overloaded methods with different parameter lists.
  • icon Predict which overloaded method will execute based on arguments.
  • icon Apply overloading to create flexible, readable methods.
divider

Activity Tasks

  • icon Create a new Java file named 3-4-Method-Overloading.

Task 1: Basic Overloading

  • icon Define three versions of an add() method:
    • add(int a, int b)
    • add(double a, double b)
    • add(int a, int b, int c)
  • icon Call each version from main() and print the results.
Program.java
public class Program
{
public static void main(String[] args)
{
System.out.println(add(4, 5));
System.out.println(add(4.5, 2.3));
System.out.println(add(2, 3, 4));
}
static int add(int a, int b)
{
return a + b;
}
static double add(double a, double b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
}
divider

Task 2: Mixed Parameter Types

  • icon Without removing your existing methods, add two new versions:
    • add(int a, double b)
    • add(double a, int b)
  • icon Print a message in each version to see which one runs.
  • icon Test each new method call in main().
Program.java
public class Program
{
public static void main(String[] args)
{
System.out.println(add(4, 5));
System.out.println(add(4.5, 2.3));
System.out.println(add(2, 3, 4));
System.out.println(add(3, 2.5));
System.out.println(add(2.5, 3));
}
14 collapsed lines
static int add(int a, int b)
{
return a + b;
}
static double add(double a, double b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
static double add(int a, double b)
{
System.out.println("int + double version called");
return a + b;
}
static double add(double a, int b)
{
System.out.println("double + int version called");
return a + b;
}
}
divider

Task 3: Applied Overloading

  • icon Continue working in Program.java. Add three overloaded area() methods that calculate the area of different shapes:
    • area(double radius) → Circle
    • area(double width, double height) → Rectangle
    • area(double base, double height, boolean triangle) → Triangle
  • icon Call each area() method in main() and print the results.
Program.java
public class Program
{
public static void main(String[] args)
{
System.out.println(add(4, 5));
System.out.println(add(4.5, 2.3));
System.out.println(add(2, 3, 4));
System.out.println(add(3, 2.5));
System.out.println(add(2.5, 3));
System.out.println("Circle area: " + area(3.0));
System.out.println("Rectangle area: " + area(4.0, 6.0));
System.out.println("Triangle area: " + area(5.0, 3.0, true));
}
// Overloaded add() methods
26 collapsed lines
static int add(int a, int b)
{
return a + b;
}
static double add(double a, double b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
static double add(int a, double b)
{
System.out.println("int + double version called");
return a + b;
}
static double add(double a, int b)
{
System.out.println("double + int version called");
return a + b;
}
// Overloaded area() methods
static double area(double radius)
{
return Math.PI * radius * radius;
}
static double area(double width, double height)
{
return width * height;
}
static double area(double base, double height, boolean triangle)
{
return 0.5 * base * height;
}
}
divider

Sample Output

Sample Output
9
6.8
9
int + double version called
5.5
double + int version called
5.5
Circle area: 28.274333882308138
Rectangle area: 24.0
Triangle area: 7.5
divider

Reflection Questions

  1. What determines which version of an overloaded method executes?
  2. Why can't you overload a method by changing only the return type?
  3. How does overloading make code more readable and organized?
  4. Where have you seen method overloading used before in Java?
divider

Submission

Submit your completed Program.java file and reflection answers to the appropriate dropbox.

Activity Complete