Lecture 5-Methods and Method OverLoading
Lecture 5-Methods and Method OverLoading
2
Topic – 1: Methods in Java
3
Java Methods
4
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
5
Syntax of Defining a function
6
Here,
7
Categories of User-Defined Methods:
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.
19
Different ways to overload the method
20
1. By changing number of arguments
public class MethodOverloading {
• 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