METHOD OVERLOADING
Presentation by :
Vaibhav Singh Vivek Bharti Ushnik Sarkar
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.
PARTS OF A METHOD
• Access specifier : - It is optional as it tells the compiler how to call public, private, and
others specifier.
• Return Type : - sometimes a method may return a value or does not return a value (for that
we use void).
• Name of method : - This is to assign name of method.
• Parameters : - a comma-delimited list of input parameters, preceded by their data types,
enclosed by parentheses. If there are no parameters, you must use empty parentheses.
• Body of method : - The method body enclosed between braces contains a collection of
statements that define what the method does.
CREATING A METHOD
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.
• Overloading methods demonstrate the concept of polymorphism.
• 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.
DIFFERENT WAYS TO OVERLOAD
METHOD
1.) By changing the no. of arguments.
2.) By changing the data types.
EXAMPLE
//method overloading.
class Ovrld
{
void check()
{
System.out.println("No parameters");
}
// Overload check for single integer parameter
void check(int a)
{
System.out.println("a: " + a);
}
// Overload check for two integer parameters.
void check(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
// overload check for a double parameter
double check(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
//Now we create a main class Overload,
which prints all overloaded methods.
class Overload
{
public static void main(String args[])
{
Ovrld ob = new Ovrld();
double result;
// call all versions of check()
ob.check();
ob.check(10);
ob.check(10, 20);
result = ob.check(123.2);
System.out.println("Result of ob.check(123.2): " + result);
}
}
OUTPUT (EXAMPLE)
RULE OF OVERLOADING A METHOD IN
JAVA
Following rules are to be followed in method overloading
• First rule is to change method signature in method overloading. Method signature is
made of (1) number of arguments, (2) type of arguments and (3) order of arguments
if they are of different types.
• The name of method should be same for the overloaded methods.
• Return type of method is not part of method signature, so just changing the return
type will not overload method in Java.
ADVANTAGES OF METHOD OVERLOADING
IN JAVA
• Overloading in Java is the ability to create multiple methods of the same name, but with different
parameters.
• The main advantage of this is cleanliness of code.
• Method overloading increases the readability of the program.
• Overloaded methods give programmers the flexibility to call a similar method for different types of
data.
• Overloading is also used on constructors to create new objects given different amounts of data.
• You must define a return type for each overloaded method. Methods can have different return types
THANK YOU

Method overloading

  • 1.
    METHOD OVERLOADING Presentation by: Vaibhav Singh Vivek Bharti Ushnik Sarkar
  • 2.
    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.
  • 3.
    PARTS OF AMETHOD • Access specifier : - It is optional as it tells the compiler how to call public, private, and others specifier. • Return Type : - sometimes a method may return a value or does not return a value (for that we use void). • Name of method : - This is to assign name of method. • Parameters : - a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses. If there are no parameters, you must use empty parentheses. • Body of method : - The method body enclosed between braces contains a collection of statements that define what the method does.
  • 4.
  • 5.
    METHOD OVERLOADING •Two ormore 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. • Overloading methods demonstrate the concept of polymorphism. • 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.
  • 6.
    DIFFERENT WAYS TOOVERLOAD METHOD 1.) By changing the no. of arguments. 2.) By changing the data types.
  • 7.
    EXAMPLE //method overloading. class Ovrld { voidcheck() { System.out.println("No parameters"); } // Overload check for single integer parameter void check(int a) { System.out.println("a: " + a); } // Overload check for two integer parameters. void check(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload check for a double parameter double check(double a) { System.out.println("double a: " + a); return a*a; } } //Now we create a main class Overload, which prints all overloaded methods. class Overload { public static void main(String args[]) { Ovrld ob = new Ovrld(); double result; // call all versions of check() ob.check(); ob.check(10); ob.check(10, 20); result = ob.check(123.2); System.out.println("Result of ob.check(123.2): " + result); } }
  • 8.
  • 9.
    RULE OF OVERLOADINGA METHOD IN JAVA Following rules are to be followed in method overloading • First rule is to change method signature in method overloading. Method signature is made of (1) number of arguments, (2) type of arguments and (3) order of arguments if they are of different types. • The name of method should be same for the overloaded methods. • Return type of method is not part of method signature, so just changing the return type will not overload method in Java.
  • 10.
    ADVANTAGES OF METHODOVERLOADING IN JAVA • Overloading in Java is the ability to create multiple methods of the same name, but with different parameters. • The main advantage of this is cleanliness of code. • Method overloading increases the readability of the program. • Overloaded methods give programmers the flexibility to call a similar method for different types of data. • Overloading is also used on constructors to create new objects given different amounts of data. • You must define a return type for each overloaded method. Methods can have different return types
  • 11.