Method Overloading in Java

Method overloading in Java is a feature that allows a class to have more than one method with the same name, provided their parameter lists (also known as method signatures) are different. This is a type of polymorphism (specifically, compile-time or static polymorphism) where multiple methods can perform similar but slightly varied tasks, differentiated by the number and type of their parameters.

Key Points of Method Overloading:

  1. Same Method Name: The overloaded methods must have the same name.
  2. Different Parameter List: The parameter list must differ in at least one of the following ways:
    • Number of parameters
    • Type of parameters
    • Order of parameters (if the types are different)
  3. Return Type: The return type can be different, but it does not contribute to the method signature for overloading purposes.

Benefits of Method Overloading:

  • Readability: Makes the code easier to read and understand by using the same method name for similar actions.
  • Reusability: Encourages code reuse by allowing a common method name for related functionalities.
  • Flexibility: Provides the ability to call the same method with different types or numbers of arguments.

Example of Method Overloading:

Here's a simple example to demonstrate method overloading:

public class MethodOverloadingExample {

    // Method to add two integers
    public int add(int a, int b) {
        return a + b;
    }

    // Method to add three integers
    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // Method to add two double values
    public double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        MethodOverloadingExample example = new MethodOverloadingExample();

        // Calling the first add method
        System.out.println("Sum of two integers: " + example.add(10, 20));

        // Calling the second add method
        System.out.println("Sum of three integers: " + example.add(10, 20, 30));

        // Calling the third add method
        System.out.println("Sum of two doubles: " + example.add(10.5, 20.5));
    }
}

Explanation:

  • First add Method: Takes two integers as parameters and returns their sum.
  • Second add Method: Takes three integers as parameters and returns their sum.
  • Third add Method: Takes two double values as parameters and returns their sum.
  • Main Method: Demonstrates calling each of the overloaded add methods.

Key Takeaway:

Method overloading enhances the readability and usability of code by allowing multiple methods with the same name to exist within a class, each performing similar but distinct tasks based on the parameters passed to them. This leads to cleaner and more intuitive code.

Program to try:Write a Java program to calculate the area of different shapes namely circle, rectangle, and triangle using the concept of method overloading.

public class AreaCalculator {

    // Method to calculate the area of a circle
    public double calculateArea(double radius) {
        return Math.PI * radius * radius;
    }

    // Method to calculate the area of a rectangle
    public double calculateArea(double length, double width) {
        return length * width;
    }

 
    // Method to calculate the area of a triangle using three sides (Heron's formula)
    public double calculateArea(double a, double b, double c) {
        // Calculate the semi-perimeter
        double s = (a + b + c) / 2;

        // Calculate the area using Heron's formula
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

        return area;
    }

    public static void main(String[] args) {
        AreaCalculator calculator = new AreaCalculator();

        // Calculate and print the area of a circle
        double circleArea = calculator.calculateArea(5.0);
        System.out.println("Area of the circle: " + circleArea);

        // Calculate and print the area of a rectangle
        double rectangleArea = calculator.calculateArea(4.0, 6.0);
        System.out.println("Area of the rectangle: " + rectangleArea);

      
        // Calculate and print the area of a triangle (three sides)
        double triangleAreaBySides = calculator.calculateArea(3.0, 4.0, 5.0);
        System.out.println("Area of the triangle (three sides): " + triangleAreaBySides);
    }
}


Comments

Popular posts from this blog

KTU OOP LAB JAVA CSL 203 BTech CS S3 - Dr Binu V P

Syllabus and Practice Questions KTU OOPS Lab Java CSL 203

String Problems