Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JAVA Method Overloading

  • • When a particular method is defined multiple times in a class to perform multiple tasks, is known as method overloading.
  • • Here method name is same but they are different by their signatures, that means method will take different types of arguments. Otherwise compile-time error will arise.
  • • Method overloading is an example of compile-time polymorphism.
  • • When an overloaded method is invoked, JVM invokes the method according to the type and/or number of arguments that the method is taking.
OverloadingExample1.java
class Test {
void show() {
	System.out.println("JAVA means SILAN Technology");
}
void show(int x) {
	System.out.println("x=" + x);
}
void show(double p, double q) {
	System.out.println(p + " " + q);
}

class OverloadingExample1 {
	public static void main(String[] args) {
		Test t = new Test();
		t.show();
		t.show(100);
		t.show(5.2, 4.6);
	}
}

Output:
img <


Another Example;

OverloadingExample2.java

class AreaExamples {
void area(int length, int breadth) {
	int a;
	a = length * breadth;
	System.out.println("area of rectangle is" + a);
}
void area(double base, double height) {
	double ar;
	ar = 0.5 * base * height;
	System.out.println("area of triangle is" + ar);
}
void area(double r) {
	double arr;
	arr = 3.141 * r * r;
	System.out.println("area of circle is" + arr);
}

class OverloadingExample2{
	public static void main(String[] args)
   	{
		AreaExamples ob=new AreaExamples();
		ob.area(5,6);
		ob.area(2.5,3.4);
		ob.area(4.6);
   	}
}

Output:
img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext