Methods Overloading in Java

Vinnu Mukhi
May 27, 2021

Example of Void method

/*When we don't want our method to return anything, we use void as the return type*/public class Methods {static void telljoke(){
System.out.println("Hello It's a joke!");
}

public static void main(String[] args) {
telljoke();
}
}
Output: Hello Its a joke!

Method Overloading

public class Methods {
static void foo() {
System.out.println("Good Morning Bro!");
}
static void foo(int a) {
System.out.println("Good Morning " + a + " Bro");
}
static void foo(int a, int b){
System.out.println("Good morning " + a +" "+ b+ " Bro");
}

public static void main(String[] args) {
foo();
foo(100);
foo(200,300);
//Method overloading can't be performed by changing the return type of methods.
}
}
Output: Good Morning Bro!
Good Morning 100 Bro
Good morning 200 300 Bro

--

--

Vinnu Mukhi
0 Followers

I'm a Computer Science Student from Indore (India) and I'm Sharing all my learnings here.