Method Overloading with Different Data Types in Java


Write a Java program to demonstrate method overloading with two methods having different data types

This Java code demonstrates method overloading, a feature that allows a class to have multiple methods with the same name but different parameters. In this example, the OverloadExample class has two print methods, each accepting a different type of parameter: one for int and one for double.

  • OverloadExample class: This is a class definition named OverloadExample.
  • print(int num) method: This method takes an integer parameter num and prints "Integer : " followed by the value of num.
  • print(double num) method: This method takes a double parameter num and prints "Double : " followed by the value of num.
  • main method: This is the entry point of the program. It creates an instance of the OverloadExample class named obj. Then it calls the print method twice, once with an integer argument 18 and once with a double argument 84.44.

Source Code

class OverloadExample
{
	void print(int num)
	{
		System.out.println("Integer : " + num);
	}
 
	void print(double num)
	{
		System.out.println("Double : " + num);
	}
 
	public static void main(String[] args)
	{
		OverloadExample obj = new OverloadExample();
		obj.print(18);
		obj.print(84.44);
	}
}

Output

Integer : 18
Double : 84.44

Example Programs