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

Lecture 5-Methods and Method OverLoading

This document discusses methods in Java and method overloading. It defines methods as collections of statements that perform specific tasks. There are two types of methods: standard library methods that are built-in to Java, and user-defined methods that are created based on requirements. Method overloading allows a class to have multiple methods with the same name but different parameters. It increases program readability. Method overloading is achieved by changing the number of arguments or data types of arguments, but not by only changing the return type.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Lecture 5-Methods and Method OverLoading

This document discusses methods in Java and method overloading. It defines methods as collections of statements that perform specific tasks. There are two types of methods: standard library methods that are built-in to Java, and user-defined methods that are created based on requirements. Method overloading allows a class to have multiple methods with the same name but different parameters. It increases program readability. Method overloading is achieved by changing the number of arguments or data types of arguments, but not by only changing the return type.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Lecture – 5

Method and Method Overloading


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 Overloading
– 2: Method 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


{ overloaded methods, first sum method
System.out.println(a+b); performs addition of two numbers and
} second sum method performs addition
  void sum(int a,int b,int c) 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
void sum(int a,int b) two 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
Why Method Overloading is not possible by changing the return type of method only?

In java, method overloading is impossible by changing the method's return type only
because of ambiguity. Let's see how ambiguity may occur:

class Adder{  
static int add(int a,int b){return a+b;}  
static double add(int a,int b){return a+b;}  
}  
class Test{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));//ambiguity  
}}  //compile time error
24
 See the following code line:
System.out.println(Adder.add(11,11)); //Here, how can java determine which
sum() method should be called?
So java compiler renders compiler time error if you declare the same method
having same parameters.

25
Can we overload java main() method?
 Yes, by method overloading. You can have any number of main methods in a
class by method overloading. But JVM calls main() method which receives
string array as arguments only. Let's see the simple example:

class Test{  
public static void main(String[] args){System.out.println("main with String[]");}  
public static void main(String args){System.out.println("main with String");}  
public static void main(){System.out.println("main without args");}  
}  

26
Important Points to remember

 Two or more methods can have the same name inside the same class if they
accept different arguments. This feature is known as method overloading.
 Method overloading is achieved by either:
 changing the number of arguments.
 or changing the data type of arguments.
 It is not method overloading if we only change the return type of methods.
There must be differences in the number of parameters.

27
Thank you!

28

You might also like