A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Java program to implement method overloading based on the order of arguments
Q:

Java program to implement method overloading based on the order of arguments

0

Java program to implement method overloading based on the order of arguments

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create 3 methods with the same name but all methods have different order of arguments to calculate the sum of given arguments.

Program/Source Code:

The source code to implement method overloading based on the order of arguments is given below. The given program is compiled and executed successfully.

// Java program to implement method overloading based 
// on the order of arguments

public class Main {
  static float sum(int num1, int num2) {
    int s = 0;
    s = num1 + num2;
    return s;
  }
  
  static float sum(int num1, float num2) {
    float s = 0;
    s = num1 + num2;
    return s;
  }

  static float sum(float num1, int num2) {
    float s = 0;
    s = num1 + num2;
    return s;
  }

  public static void main(String[] args) {
    float result = 0;

    result = sum(10, 20);
    System.out.println("Sum : " + result);

    result = sum(20, 20.56F);
    System.out.println("Sum : " + result);

    result = sum(30.45F, 34);
    System.out.println("Sum : " + result);
  }
}

Output:

Sum : 30.0
Sum : 40.559998
Sum : 64.45

Explanation:

In the above program, we created a Main class that contains a method main() and 3 overloaded method sum() to calculate the addition of specified different order of arguments.

The main() method is the entry point for the program, here we called overloaded methods and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now