878198. Method Overloading in Java

Easy

Method Overloading in Java

Slug: method-overloading-in-java Difficulty: Easy Id: 878198 Topic Tags: None Company Tags: None

Summary

This problem is about understanding how to use method overloading in Java. Method overloading, also known as compile-time polymorphism, allows a class to have multiple methods with the same name but different parameter lists. This concept helps in providing better code readability and flexibility.

Detailed Explanation

Method overloading is used when we want to perform an operation on different data types. For example, consider you are writing a program that calculates the area of different shapes like rectangle, circle, etc. You can define a method called calculateArea() with different parameters for each shape. This way, you don't have to give up a specific name but still provide different functionality based on the parameters.

Here's an example of how we can achieve this:

public class Shape {
    public double calculateArea(int length, int breadth) {
        return length * breadth;
    }

    public double calculateArea(double radius) {
        return 3.14 * Math.pow(radius, 2);
    }
}

In this example, Shape is a class that has two methods with the same name but different parameters: calculateArea(int, int) and calculateArea(double). These are method overloads.

Optimized Solutions

Java

View on GeeksforGeeks
public double calculateArea(int length, int breadth) {
    return length * breadth;
}

public double calculateArea(double radius) {
    return 3.14 * Math.pow(radius, 2);
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.