Pundra University of Science &
Technology
Presentation on
Method Overloading in Java
Submitted by
Name: Md.Delowar
Hossain Dolon
ID: 00817206031
Batch: 2nd
Semester: 5th
Department of CSE
Submitted to
Name: Syed Mir
Talha Zobaed
Designation: Lecturer
in CSE
Department of CSE
Outline
 What is signature?
 What is Polymorphism?
 What is Method?
Method Overloading
 Why overload a method?
Example of Method Overloading
• .
What is signature?
In any programming language, a signature is
what distinguishes one function or method
from another
In C, every function has to have a different
name
 In Java, two methods have to differ in their
names or in the number or types of their
parameters
 Same name(int i) and (int i, int j) are different
 Same name(int i) and (int k) are the same
 Same name(int i, double d) and (double d, int i) are
different
In C++, the signature also includes the
return type
But not in Java!
What is Polymorphism?
 Polymorphism means many (poly) shapes
(morph)
 In Java, polymorphism refers to the fact that
we can multiple methods with the same name
in the same class
 There are two kinds of polymorphism:
i. Overloading
◦ Two or more methods with different signatures
ii. Overriding ◦
Replacing an inherited method with another having the same
signature
What is Method?
 A Java method is a collection of
statements that are grouped together to
perform an operation.
 A class can contain any number of
methods.
 Methods can be with parameter and
without parameter.
 The parameter in a method are called
type signature.
Method Overloading
 Two or more methods within the same class
that share the same name, but with different
parameter declarations (type signatures).
 The process is referred to as method
overloading.
 Overloaded methods may have different return
types.
 When java encounters a call to an overloaded
method, it simply executes the version of the
method whose parameters match the
arguments used in the call.
Why overload a method?
 So we can use the same names for methods that do essentially the
same thing
 Example: println(int), println(double), println(boolean), println(String), etc.
 So we can supply defaults for the parameters:
int increment(int amount) {
count = count + amount;
return count;
}
int increment() {
return increment(1);
}
 Notice that one method can call another of the same name
 So we can supply additional information:
void printResults() {
System.out.println("total = " + total + ", average = " + average);
}
void printResult(String message) {
System.out.println(message + ": ");
printResults();
}
Example
class Test
{
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) { // same name, different parameters
System.out.println("double d = " + d);
}
}
int i = 5
double d = 5.0
.

Method Overloading in Java

  • 1.
    Pundra University ofScience & Technology Presentation on Method Overloading in Java Submitted by Name: Md.Delowar Hossain Dolon ID: 00817206031 Batch: 2nd Semester: 5th Department of CSE Submitted to Name: Syed Mir Talha Zobaed Designation: Lecturer in CSE Department of CSE
  • 2.
    Outline  What issignature?  What is Polymorphism?  What is Method? Method Overloading  Why overload a method? Example of Method Overloading
  • 3.
    • . What issignature? In any programming language, a signature is what distinguishes one function or method from another In C, every function has to have a different name  In Java, two methods have to differ in their names or in the number or types of their parameters  Same name(int i) and (int i, int j) are different  Same name(int i) and (int k) are the same  Same name(int i, double d) and (double d, int i) are different In C++, the signature also includes the return type But not in Java!
  • 4.
    What is Polymorphism? Polymorphism means many (poly) shapes (morph)  In Java, polymorphism refers to the fact that we can multiple methods with the same name in the same class  There are two kinds of polymorphism: i. Overloading ◦ Two or more methods with different signatures ii. Overriding ◦ Replacing an inherited method with another having the same signature
  • 5.
    What is Method? A Java method is a collection of statements that are grouped together to perform an operation.  A class can contain any number of methods.  Methods can be with parameter and without parameter.  The parameter in a method are called type signature.
  • 6.
    Method Overloading  Twoor more methods within the same class that share the same name, but with different parameter declarations (type signatures).  The process is referred to as method overloading.  Overloaded methods may have different return types.  When java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
  • 7.
    Why overload amethod?  So we can use the same names for methods that do essentially the same thing  Example: println(int), println(double), println(boolean), println(String), etc.  So we can supply defaults for the parameters: int increment(int amount) { count = count + amount; return count; } int increment() { return increment(1); }  Notice that one method can call another of the same name  So we can supply additional information: void printResults() { System.out.println("total = " + total + ", average = " + average); } void printResult(String message) { System.out.println(message + ": "); printResults(); }
  • 8.
    Example class Test { public staticvoid main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(int i) { System.out.println("int i = " + i); } static void myPrint(double d) { // same name, different parameters System.out.println("double d = " + d); } } int i = 5 double d = 5.0
  • 9.