Computer Applications
Discuss how the best match is found when a call to an overloaded method is encountered. Give example(s) to support your answer.
User Defined Methods
1 Like
Answer
When an overloaded function is called, the compiler matches the signature in the function definitions with the arguments given in the method call statement and executes the function once the match is found.
To avoid ambiguity, we can use constant suffixes (F, L, D, etc.) to distinguish between values passed as arguments. These greatly help in indicating which overloaded function should be called. For example, an ordinary floating constant (e.g., 312.32) has the double type, while adding the F suffix (e.g., 312.32 F) makes it a float.
For example, the following code overloads a function perimeter() which calculates the perimeter of square, rectangle and trapezium.
public class Perimeter
{
public double perimeter(double s)
{
double p = 4 * s;
return p;
}
public double perimeter(double l,
double b)
{
double p = 2 * (l + b);
return p;
}
public double perimeter(double a,
double b,
double c,
double d)
{
double p = a + b + c + d;
return p;
}
public static void main(String args[]) {
Perimeter obj = new Perimeter();
double areaSquare = obj.perimeter(5.8);
double areaRectangle = obj.perimeter(10.5, 4.5);
double areaTrapezium = obj.perimeter(4, 10, 7, 15);
System.out.println(areaSquare + " "
+ areaRectangle + " "
+ areaTrapezium);
}
}
Output
23.2 30.0 36.0
Answered By
1 Like
Related Questions
Write a program that uses a method power( ) to raise a number m to power n. The method takes int values for m and n and returns the result correctly. Use a default value of 2 for n to make the function calculate squares when this argument is omitted. Write a main( ) method to get the value of m and n to display the calculated result.
How does the compiler interpret more than one definitions having same name ? What steps does it follow to distinguish these ?
Design a class to overload a function area( ) as follows:
- double area (double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
area = √(s(s-a)(s-b)(s-c))
where s = (a+b+c) / 2 - double area (int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
area = (1/2)height(a + b) - double area (double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula:
area = 1/2(diagonal1 x diagonal2)
- double area (double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
What is the output of the following program?
class AllStatic { static int m = 0 ; static int n = 0 ; public static void main(String[ ] args) { int m = 10; int x = 20; { int n = 30 ; System.out.println("m + n =" + m + n) ; check(5) ; } x = m + n; System.out.println("x =" + x) ; } public static void check int k ; { int m = 5 ; n = k; System.out.println("m is " + m) ; System.out.println("n is " + n) ; } }