Monday, April 28, 2025

Java Tutorial - Method Overloading

Method overloading in Java is a feature that allows a class to have multiple methods with the same name, but with different parameters. This means that you can define two or more methods with the same name in a class, as long as they have different parameter lists.

In method overloading, the compiler determines which version of the method to use based on the number, types, and order of the arguments passed to it. When you call an overloaded method, the compiler chooses the appropriate method to execute based on the arguments you pass to it. 

public class Main {
	
	public static int sameName(int x, int y) {
		return x + y;
	}
	
	public static String sameName(String x, String y) {
		return x + " " + y;
	}
	
	public static double sameName(double x, double y) {
		return x + y;
	}
	
	public static void main(String[] args) {
		
		System.out.println(sameName(5, 10));
		System.out.println(sameName("asdas", "asdasda"));
		
		System.out.println(sameName(5.0, 10.0));		
	}	
}

Here's an explanation of the code block by block:

  1. We begin with defining a Java class called Main
public class Main {
  1. Inside the Main class, we define three static methods with the same name sameName
public static int sameName(int x, int y) {
	return x + y;
}

public static String sameName(String x, String y) {
	return x + " " + y;
}

public static double sameName(double x, double y) {
	return x + y;
}
  1. Each of the sameName methods has a different signature, which means they take different types of parameters. The first method takes two int parameters, the second method takes two String parameters, and the third method takes two double parameters.

  2. Inside each method, we simply perform a simple arithmetic operation (+ operator for int and double parameters) or concatenate the String parameters with a space.

  3. In the main method, we call the sameName methods with different arguments and print the returned results: 

System.out.println(sameName(5, 10));
System.out.println(sameName("asdas", "asdasda"));
System.out.println(sameName(5.0, 10.0));		
  1. The first call to sameName passes two int arguments (5 and 10) and returns an int value (15). This is printed to the console.

  2. The second call to sameName passes two String arguments ("asdas" and "asdasda") and returns a String value ("asdas asdasda"). This is also printed to the console.

  3. The third call to sameName passes two double arguments (5.0 and 10.0) and returns a double value (15.0). This is also printed to the console.

In summary, this code demonstrates how method overloading works in Java by defining multiple methods with the same name but different parameter types, and then calling them with different arguments to perform different tasks.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...