SlideShare a Scribd company logo
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

More Related Content

What's hot

My first experience with lambda expressions in java
My first experience with lambda expressions in javaMy first experience with lambda expressions in java
My first experience with lambda expressions in java
Scheidt & Bachmann
 

What's hot (20)

Java in mule part 3
Java in mule part 3Java in mule part 3
Java in mule part 3
 
Java Tutorial Lab 5
Java Tutorial Lab 5Java Tutorial Lab 5
Java Tutorial Lab 5
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercises
 
CMSC 350 HOMEWORK 1
CMSC 350 HOMEWORK 1CMSC 350 HOMEWORK 1
CMSC 350 HOMEWORK 1
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overriding
 
Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3
 
Delegate
DelegateDelegate
Delegate
 
Java Tutorial Lab 8
Java Tutorial Lab 8Java Tutorial Lab 8
Java Tutorial Lab 8
 
Java 8
Java 8Java 8
Java 8
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
My first experience with lambda expressions in java
My first experience with lambda expressions in javaMy first experience with lambda expressions in java
My first experience with lambda expressions in java
 
Files io
Files ioFiles io
Files io
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
30csharp
30csharp30csharp
30csharp
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Java Tutorial Lab 2
Java Tutorial Lab 2Java Tutorial Lab 2
Java Tutorial Lab 2
 

Similar to Method overloading

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 

Similar to Method overloading (20)

Method overloading
Method overloadingMethod overloading
Method overloading
 
11. java methods
11. java methods11. java methods
11. java methods
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Effective Java - Design Method Signature Overload Varargs
Effective Java - Design Method Signature Overload VarargsEffective Java - Design Method Signature Overload Varargs
Effective Java - Design Method Signature Overload Varargs
 
Lec 1.4 Object Oriented Programming
Lec 1.4 Object Oriented ProgrammingLec 1.4 Object Oriented Programming
Lec 1.4 Object Oriented Programming
 
UNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxUNIT_-II_2021R.pptx
UNIT_-II_2021R.pptx
 
Working with Methods in Java.pptx
Working with Methods in Java.pptxWorking with Methods in Java.pptx
Working with Methods in Java.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
U2 JAVA.pptx
U2 JAVA.pptxU2 JAVA.pptx
U2 JAVA.pptx
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Class 10
Class 10Class 10
Class 10
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Session 08 - Arrays and Methods
Session 08 - Arrays and MethodsSession 08 - Arrays and Methods
Session 08 - Arrays and Methods
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Java8
Java8Java8
Java8
 
Dreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and TricksDreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and Tricks
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 

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 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.
  • 5. 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.
  • 6. DIFFERENT WAYS TO OVERLOAD METHOD 1.) By changing the no. of arguments. 2.) By changing the data types.
  • 7. 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); } }
  • 9. 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.
  • 10. 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