Lecture – 5
Method and Method Overloading
Afsara Tasneem Misha
Lecturer
Department of CSE
Daffodil International University
Contents
• Method in Java
• Method Overloading
2
Topic – 1: Methods in Java
3
Java Methods
• Java Method is a collection of statements that are grouped together to
perform a specific task.
• A method must be declared within a class.
4
Types of Methods:
In Java, there are two types of methods:
• Standard Library Methods: These are built-in methods in Java that are
available to use. Such as, print(), nextInt(), sqrt() and many more
• User-defined Methods: We can create our own method based on our
requirements.
5
Syntax of Defining a function
modifier return_type methode_name (parameter list)
{
// body of the method
}
6
Here,
• Modifier: it defines the access type of the method
• Return_type: specifies the type of data returned by the method
• Methode_name: name of the method
• Parameter_list: list of parameters. It is the type, order and number of
parameters of the method
• Body_of_method: defines what the method does
7
Categories of User-Defined Methods:
For better understanding of arguments and return type in methods,
user-defined methods can be categorized as:
1. Method with no arguments and no return value
2. Method with arguments but no return value
3. Method with no arguments but with return value
4. Method with arguments and return value.
8
Example -1: Add two integer values using method
9
A method is always invoked with respect to an object.
10
Example- 2:
• Define a method called max(). This method takes two parameters num1
and num2 and returns the maximum between the two.
Try yourself!
11
Solution 1:
*Without using object
If we want to call any method
without any object,
then we have to declare
the method as static.
12
Solution 2:
*Using object
13
Example-3 : Convert this UML to Java Code
14
Solution
15
Visit the links for more details:
• https://www.javatpoint.com/method-in-java
• https://www.tutorialspoint.com/java/java_methods.htm
• https://www.w3schools.com/java/java_methods.asp
16
Topic –Method
2: MethodOverloading
Overloading in Java
17
Try?
• Write a program that will add two integer numbers, and three integer
numbers using two different methods and display two result.
• Write a program that will add two integer numbers, and two double numbers
two different methods and display two result.
18
Method Overloading
• When a class has two or more methods by same name but different
parameters, it is known as method overloading.
• Method overloading increases the readability of the program.
19
Different ways to overload the method
• There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
20
1. By changing number of arguments
public class MethodOverloading {
void sum(int a, int b)
{ In this example, we have created two
System.out.println(a+b); overloaded methods, first sum method
} performs addition of two numbers and
void sum(int a, int b, int c) second sum method performs addition
{
of three numbers.
System.out.println(a+b+c);
}
public static void main(String args[]){
MethodOverloading obj = new MethodOverloading();
obj.sum(20,20);
obj.sum(10,20,30);
}
}
21
2. By changing data type of argument
public class MethodOverloading2 {
In this example, we have created two
void sum(int a,int b) overloaded methods that differs in
{ data type. The first sum method
System.out.println(a+b); receives two integer arguments and
} second sum method receives two
double arguments.
void sum(double a, double b)
{
System.out.println(a+b);
}
public static void main(String args[]){
MethodOverloading2 obj=new MethodOverloading2();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
} 22
Exercise – 1
• Find out the maximum value between two integer numbers, three integer
numbers and two double numbers.
23
Thank you!
24