KEMBAR78
Methods Overloading | PDF | Parameter (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
9 views22 pages

Methods Overloading

Method Overloading allows a class to have multiple methods with the same name but different argument lists, which can differ in the number, data type, or sequence of parameters. It is important to note that changing the return type alone does not constitute valid overloading. Examples demonstrate various valid and invalid cases of method overloading, emphasizing that overloaded methods must have different parameter signatures.

Uploaded by

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

Methods Overloading

Method Overloading allows a class to have multiple methods with the same name but different argument lists, which can differ in the number, data type, or sequence of parameters. It is important to note that changing the return type alone does not constitute valid overloading. Examples demonstrate various valid and invalid cases of method overloading, emphasizing that overloaded methods must have different parameter signatures.

Uploaded by

pyvbvaraprasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Method Overloading

• Method Overloading is a feature that allows a class to have more than one method having the same
name, if their argument lists are different.
• For example the argument list of a method add(int a, int b) having two parameters is different from
the argument list of the method add(int a, int b, int c) having three parameters.
• Three ways to overload a method
1. Number of parameters.
add(int, int)
add(int, int, int)

2. Data type of parameters.


add(int, int)
add(int, float)

3. Sequence of Data type of parameters.


add(int, float)
add(float, int)
public static double multiply(double x, double y)
{
return (x * y);
}

public static double multiply(double x, double y, double z)


{
return (x * y * z);
}

Invalid case of method overloading:

When I say argument list, I am not talking about return type of the method, for example if two methods have
same name, same parameters and have different return type, then this is not a valid method overloading

example. This will throw compilation error.

int add(int, int)


float add(int, int)
void func() { ... }

void func(int a) { ... }

float func(double a) { ... }

float func(int a, float b) { ... }

Here, func() method is overloaded. These methods have same name but accept different arguments.

Notice that, the return type of these methods are not same.

Overloaded methods may or may not have different return type, but they must differ in parameters
they accept.
1. Overloading by changing number of arguments

class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}

private static void display(int a, int b){


System.out.println("Arguments: " + a + " and " + b);
}

public static void main(String[] args) {


display(1);
display(1, 4);
}
}

Arguments: 1
Arguments: 1 and 4
Got Integer data. Got String object.

2. By changing the datatype of parameters

class MethodOverloading {

// this method accepts int


private static void display(int a){
System.out.println("Got Integer data.");
}

// this method accepts String object


private static void display(String a){
System.out.println("Got String object.");
}

public static void main(String[] args) {


display(1);
display("Hello");
}
} Got Integer data.
Got String object.
Important Points

Two or more methods can have same name inside the same class if they accept different arguments. This
feature is known as method overloading.

Method overloading is achieved by either:

changing the number of arguments.

or changing the datatype of arguments.

Method overloading is not possible by changing the return type of methods.


// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// Overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test (123.2): 15178.24

• The first version takes no parameters, the second takes one integer parameter, the third takes
two integer parameters, and the fourth takes one double parameter.
• The fact that the fourth version of test() also returns
a value is of no consequence relative to overloading, since return types do not play a role in
overload resolution.
class DemonstrateOverloading{
public static void main(String[] args){
/* Display and call sum method with two argument */
System.out.println("Sum of two number is :" +
DemonsOverloading.sum(2,3));

/* Display and call sum method with three argument */


System.out.println("Sum of three number is :" +
DemonsOverloading.sum(2,3,4));

/* Display and call sum method with three argument */


System.out.println("Sum of four number is :" +
DemonsOverloading.sum(2,3,4,5));

/* Display and call sum method with four argument */


System.out.println("Sum of five number is :" +
DemonsOverloading.sum(2,3,4,5,6));
}
}
class DemonsOverloading{
/* Definition of sum() method with two argument */
static int sum(int a, int b){
return(a+b);
}

/* Definition of sum() method with three argument */


static int sum(int a, int b,int c){
return(a+b+c);
}

/* Definition of sum() method with four argument */


static int sum(int a, int b,int c,int d){
return(a+b+c+d);
}

/* Definition of sum() method with five argument */


static int sum(int a, int b,int c,int d,int e){ Sum of two number is :5
return(a+b+c+d+e); Sum of three number is :9
Sum of four number is :14
}
Sum of five number is :20
}
Overloading – Different Number of parameters in argument list

class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading(); a
obj.disp('a'); a 10
obj.disp('a',10);
}
}
Overloading – Difference in data type of parameters
class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}

class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2(); a
obj.disp('a'); 5
obj.disp(5);
}
}
Overloading – Sequence of data type of arguments
class DisplayOverloading3
{
public void disp(char c, int num)
{
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c)
{
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 ); I’m the first definition of method disp
obj.disp(52, 'y'); I’m the second definition of method
} disp
}
Method Overloading and Type Promotion
class Demo{
void disp(int a, double b){
System.out.println("Method A");
}
void disp(int a, double b, double c){
System.out.println("Method B");
}
public static void main(String args[]){
Demo obj = new Demo();
/* I am passing float value as a second argument but
* it got promoted to the type double, because there
* wasn't any method having arg list as (int, float)
*/
obj.disp(100, 20.67f);
}
}
Method A
Method Overloading and Type Promotion
class Demo{
void disp(int a, double b){
System.out.println("Method A");
}
void disp(int a, double b, double c){
System.out.println("Method B");
}
void disp(int a, float b){
System.out.println("Method C");
}
public static void main(String args[]){
Demo obj = new Demo();
/* This time promotion won't happen as there is
* a method with arg list as (int, float)
*/
obj.disp(100, 20.67f);
} Method C
}
As you see that this time type promotion didn’t happen because there was a method with matching argument type.
Type Promotion table:
The data type on the left side can be promoted to the any of the data type present in the right side of it.

byte → short → int → long


short → int → long
int → long → float → double
float → double
long → float → double
Case 1:

int mymethod(int a, int b, float c)


int mymethod(int var1, int var2, float var3)
Result: Compile time error. Argument lists are exactly same. Both methods are having same number, data types and same
sequence of data types.

Case 2:

int mymethod(int a, int b)


int mymethod(float var1, float var2)
Result: Perfectly fine. Valid case of overloading. Here data types of arguments are different.

Case 3:

int mymethod(int a, int b)


int mymethod(int num)
Result: Perfectly fine. Valid case of overloading. Here number of arguments are different.
Case 4:

float mymethod(int a, float b)


float mymethod(float var1, int var2)
Result: Perfectly fine. Valid case of overloading. Sequence of the data types of parameters are different, first method is
having (int, float) and second is having (float, int).

Case 5:

int mymethod(int a, int b)


float mymethod(int var1, int var2)
Result: Compile time error. Argument lists are exactly same. Even though return type of methods are different, it is not a
valid case. Since return type of method doesn’t matter while overloading a method.
Question 1 – return type, method name and argument list same.
class Demo
{
public int myMethod(int num1, int num2)
{
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2)
{
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
}
class Sample4 Answer:
{ It will throw a compilation error:
public static void main(String args[]) More than one method with same
{ name and argument list cannot be
Demo obj1= new Demo(); defined in a same class.
obj1.myMethod(10,10);
obj1.myMethod(20,12);
}
}
Question 2 – return type is different. Method name & argument list same.

class Demo2
{
public double myMethod(int num1, int num2)
{
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2)
{
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
}
class Sample5
{
public static void main(String args[])
{
Demo2 obj2= new Demo2(); Answer:
obj2.myMethod(10,10); It will throw a compilation error: More than one method with
obj2.myMethod(20,12); same name and argument list cannot be given in a class even
} though their return type is different. Method return type
} doesn’t matter in case of overloading.
1.Write a program to compute area of a circle and volume of a sphere with
same radius value using Method.

2.Write a program to computes income tax using method


according to the rate schedule:
No tax on first $15,000 of income

5% tax on each dollar from $15,001


to $25,000

10% tax on each dollar over $25,000

3.Write a program for problem definition using method


Determine retail price of an item given suitable input
5% markup if item should sell in a week
10% markup if item expected to take more than a If turnover <= 7 days then
week return (cost + 5% of cost);
5% for up to 7 days, changes to 10% at 8 days else
Input return (cost + 10% of cost);
The wholesale price and the estimate of days until item sells
Output
The retail price of the item

You might also like