Method overloading in java
Method overloading is to call the same method in different different parameters.
The method "show" has a different different parameters so this is a overloading.
Example-1
class A { public void show(int i,int j) { System.out.println(i+j); } public void show(int i,int j,int k) { System.out.println(i+j+k); } } public class Main { public static void main(String[] args) { A obj = new A(); obj.show(1, 2); obj.show(5, 6, 7); } }
Output
3 18
Example-2
class A { public void show(int i,int j) { System.out.println(i+j); } public void show(int i,int j,int k) { System.out.println(i+j+k); } public void show(double i,double j) { System.out.println(i+j); } } public class Main { public static void main(String[] args) { A obj = new A(); obj.show(1, 2); obj.show(5, 6, 7); obj.show(6.5, 1.4); } }
Output
3 18 7.9