0% found this document useful (0 votes)
5 views17 pages

Method Overloading

The document discusses method overloading in Java. It defines method overloading as defining two or more methods in the same class that share the same name but differ in their parameter types and/or number of parameters. Java uses the type and number of parameters to determine which overloaded method to call. The document also discusses argument passing in Java, noting that primitive types are passed by value while objects are passed by reference. Changes to an object inside a method will affect the object used as an argument.

Uploaded by

krishna mohan G
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Method Overloading

The document discusses method overloading in Java. It defines method overloading as defining two or more methods in the same class that share the same name but differ in their parameter types and/or number of parameters. Java uses the type and number of parameters to determine which overloaded method to call. The document also discusses argument passing in Java, noting that primitive types are passed by value while objects are passed by reference. Changes to an object inside a method will affect the object used as an argument.

Uploaded by

krishna mohan G
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

K L University

Department of Computer Science and Engineering

 Object Oriented Programming


(through Java)
18SC2009R

Session
Method Overloading

Dr [Link] Mohan
Professor, Dept. CSE, KL University.
gvlkm@[Link]
class fun_overload
{
public static void main(String args[])
{
int s=3,l=4,b=5;
float r=2.5f;
double base=4.0,height=5.6;
[Link]("area of a SQUARE="+area(s));
[Link]("area of a CIRCLE="+area(r));
[Link]("area of a RECTANGLE="+area(l,b));
[Link]("area of a TRIANGLE="+area(base,height));
}
public static int area(int s)
{
return (int)([Link](s,2)); //[Link] package,
} //pow() takes double arguments and return double
//needs typecasting as return type is ‘int’
public static float area(float r)
{
final float PI=3.141f;
return (float)(PI*[Link](r,2));
}
public static int area(int l,int b)
{ return l*b; }
public static double area(double b,double h)
{ return (0.5*b*h); }
}
class fun_overload
{
public static void main(String args[])
{
int s=3,l=4,b=5;
float r=2.5f;
double base=4.0,height=5.6;
[Link]("area of a SQUARE="+area(s));
[Link]("area of a CIRCLE="+area(r));
[Link]("area of a RECTANGLE="+area(l,b));
[Link]("area of a TRIANGLE="+area(base,height));
}
public static int area(int s)
{
Removing the Block of Code
return (int)([Link](s,2)); //[Link] package,
} //pow() takes double arguments and return double
public static float area(float r)
{
final float PI=3.141f;
return (float)(PI*[Link](r,2));
}
public static int area(int l,int b)
{ return l*b; }
public static double area(double b,double h)
{ return (0.5*b*h); }
}
class fun_overload
{
public static void main(String args[])
{
int s=3,l=4,b=5;
float r=2.5f;
double base=4.0,height=5.6;
[Link]("area of a SQUARE="+area(s));
[Link]("area of a CIRCLE="+area(r));
[Link]("area of a RECTANGLE="+area(l,b));
[Link]("area of a TRIANGLE="+area(base,height));
}
public static int area(int s)
{
return (int)([Link](s,2)); //[Link] package,
} //pow() takes double arguments and return double
public static float area(float r)
{
final float PI=3.141f;
return (float)(PI*[Link](r,2));
}
public static int area(int l,int b)
{ Removing n the Block of Code
return l*b; }
public static double area(double b,double h)
{ return (0.5*b*h); }
}
class fun_overload
{
public static void main(String args[])
{
int s=3,l=4,b=5;
float r=2.5f;
double base=4.0,height=5.6;
[Link]("area of a SQUARE="+area(s));
[Link]("area of a CIRCLE="+area(r));
[Link]("area of a RECTANGLE="+area(l,b));
[Link]("area of a TRIANGLE="+area(base,height));
}
public static int area(int s)
{
return (int)([Link](s,2)); //[Link] package,
} //pow() takes double arguments and return double
public static float area(float r)
{
final float PI=3.141f;
return (float)(PI*[Link](r,2));
}
public static int area(int l,int b)
{ return l*b; }
public static double area(double b,double h)
{
}
Removing the Block of Code
return (0.5*b*h); }
Method overloading

defining two or more methods within the same class


that share the same name.
Method overloading is one of the ways that Java
supports polymorphism.
Java uses the type and/or number of arguments as
its guide to determine which version of the
overloaded method to actually call.
While overloaded methods may have different return
types, the return type alone is insufficient to
distinguish two versions of a method.
Method Overloading and Type Promotion

One type is promoted to another implicitly if no


matching data type is found.
Using Objects as Parameters
Argument Passing

there are two ways that a computer language can pass


an argument to a subroutine.
The first way is call-by-value.
 This approach copies the value of an argument into the formal
parameter of the subroutine. Therefore, changes made to the
parameter of the subroutine have no effect on the argument.
The second way is call-by-reference.
 In this approach, a reference to an argument (not the value of the
argument) is passed to the parameter. Inside the subroutine, this
reference is used to access the actual argument specified in the
call. This means that changes made to the parameter will affect
the argument used to call the subroutine.
Argument Passing

Java uses both approaches, depending upon what is


passed.
1) when you pass a primitive type to a method,
it is passed by value.

2) When you pass an object to a method,


it is passed by call-by-reference.
 Changes to the object inside the method do affect the object used
as an argument.
// Primitive types are passed by value.
class Test
{
void meth(int i, int j)
{
i *= 2;
j /= 2;
}
}
class CallByValue
{
public static void main(String args[])
{
Test ob = new Test();
int a = 15, b = 20;
[Link]("a and b before call: " +a + " " + b);
[Link](a, b);
[Link]("a and b after call: " +a + " " + b);
}
}
Returning Objects

A method can return any type of data, including class


types that you create.
Returning Objects

Common questions

Powered by AI

Relying solely on return types for method overloading is ineffective because Java does not consider the return type as part of the method signature. This can lead to a compile-time error due to ambiguity, as the Java compiler needs distinct parameter lists to differentiate between overloaded methods .

Method overloading in Java can lead to implicit type promotion when no exact match is found for a method's parameters. The compiler attempts to convert parameters into a compatible type, promoting them if necessary, to match one of the overloaded methods .

The advantage of using call-by-value for primitive types in Java is the protection it offers by preventing the original argument values from being altered within the method, ensuring data integrity. However, this also means that any changes intended to be permanent must be returned or captured outside the method, adding complexity and reducing flexibility .

When an object is passed to a Java method, it is done so by reference, allowing the method to modify the actual object attributes. This mutability stems from the fact that the method operates on the reference to the object, affecting the argument passed from outside the method .

The java.lang.Math.pow() function is utilized in the method overloading for calculating areas in Java to perform exponentiation, such as squaring numbers for area calculations of squares and circles. Although pow() returns a double, followed by conversion when necessary, its use highlights typecasting's role when differing return types are involved within overloaded methods .

Java uses call-by-value for primitive types and call-by-reference for objects. In call-by-value, a copy of the value is made, so changes within the method do not affect the original argument. Call-by-reference allows the method to access and modify the original object because a reference, rather than an actual value, is passed .

Method overloading is considered a compile-time polymorphism feature because the decision about which overloaded method to invoke is made during compilation. The compiler uses the method signature, particularly the number and type of parameters, to bind the appropriate method call statically .

Java's ability to return objects from methods enhances flexibility by allowing methods to return complex data types rather than being limited to primitive types. This capability facilitates the creation and manipulation of object instances, contributing to the development of modular and reusable code .

In Java, the method signature, which includes the method's name and the parameter list (number, type, and order of parameters), is used to determine which overloaded method to execute. The return type is not considered part of the method signature for this purpose .

Method overloading in Java allows defining two or more methods within the same class that share the same name but differ in the number or type of parameters. This concept demonstrates polymorphism by enabling the same method name to behave differently based on the provided arguments, reflecting the method's adaptability and flexibility .

You might also like