0% found this document useful (0 votes)
20 views

Java Lecture Sheet

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Lecture Sheet

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

METHOD OVERLOADING IN JAVA

BY: MD. LIMON


ID: 2212081004
Batch: 55A
INTRODUCTION
• If a class of a Java program has a plural number of methods, and all of them
have the same name but different parameters (with a change in type or
number of arguments), and programmers can use them to perform a similar
form of functions, then it is known as method overloading.
• If programmers wants to perform one operation, having the same name, the
method increases the readability of the program.
• Method Overloading is applied in a program when objects are required to
perform similar tasks but different input parameters. Every time an object
calls a method, Java matches up to the method name first and then the
number and type of parameters to decide what definitions to execute.
• This process of assigning multiple tasks to the same method is known as
Polymorphism. There are different ways to overload a method in Java.
They are:
• Based on the number of parameters: In this case, the entire overloading
concept depends on the number of parameters that are placed within the
parenthesis of the method.
• Based on the data type of the parameter: With the arrangement of data type,
within the parameter, overloading of the method can take place.
• Based on the sequence of data types in parameters: The method
overloading also depends on the ordering of data types of parameters
within the method.
SYNTEX

public class Student {


public void add(int i, int j) {

}
public void add(int i) {

}
}
PROGRAM FOR METHOD OVERLOADING

class Multiply { }
void mul(int a, int b) {
System.out.println("Sum of two=" + (a * b));
}

void mul(int a, int b, int c) {


System.out.println("Sum of three=" + (a * b * c));
}
}
public class Polymorphism {
public static void main(String args[]) {
Multiply m = new Multiply();
m.mul(6, 10);
m.mul(10, 6, 5);
}
}

OUTPUT:

Sum of two=60
Sum of three=300
Why method overloading in java is not possible by
changing the return type of method is changed?
• In Java, method overloading is not possible by changing the return type of
the method because there may arise some ambiguity. Let's see how
ambiguity may occur:

class overload RetType {


int sum(int g, int h) {
System.out.println(g+ h);
}
double sum(int g, int h) {
System.out.println(g+ h);
}
public static void main(String args[]) {
overloadRetType ob = new overload RetType();
int result = ob.sum (20, 20);
//The above line will produce a compile Time Error.
}
}

• The above program will generate a compile-time error.


• Here, Java cannot determine which sum() method to call and hence creates
an error if you want to overload like this by using the return type.
Overloading methods offer no specific benefit to the JVM, but it is useful
to the program to have several ways do the same things but with different
parameters.
OTHER EXAMPLES
class Disp0vrload
{
public void show (char ch)
{
System.out.println ("You have typed the letter: "+ch);
}
public void show(char ch, char ch1)
{
System.out.println("You have typed the letter: "+ch+" and + ch1);

}
}
class Main

{
public static void main (String args[])
{
DispOvrload o1 = new DispOvrload();
01.show('G');
01.show( 'S', 'J' );
}
}

Output:

You have typed the letter: G


You have typed the letter: S, and J
PROGRAM TO DEMONSTRATE METHOD OVERLOADING BASED
ON THE SEQUENCE OF DATA TYPE IN THE PARAMETERS

class DispOvrload
}
public void show(char ch, int numb)
{
System.out.println ("The show method' is defined for the first time.");
}
public void show(int numb, char ch)
{
System.out.println ("The 'show method' is defined for the second time." );
}
}
class Main

{
public static void main (String args[])
{
DispOvrload o1 = new DispOvrload();
01.show('G', 62);
01.show(46, 'S');
}
}

Output:
The 'show method' is defined for the first time.
The 'show method' is defined for the second time.
ADVANTAGES
• It is used to perform a task efficiently with smartness in programming.
• It increases the readability of the program.
• The Method overloading allows methods that perform proximately related
functions to be accessed using a common name with slight variation in
argument number or types.
• They can also be implemented on constructors allowing different ways to
initialize objects of a class.

DISADVANTAGES
• Not very easy for the beginner to opt this programming technique and go
with it.
• It requires more significant effort spent on designing the architecture (i.e.,
the arguments' type and number) to up front, at least if programmers' want
to avoid massive code from rewriting.
THANK YOU

You might also like