YouTip LogoYouTip

Method Overloading

# Java Example - Method Overloading [![Image 3: Java Example](#) Java Example](#) First, let's look at the definition of method overloading: If two methods have the same name but different parameters, then one method can be considered an overload of the other. The specific details are as follows: * Method name is the same * The parameter types and number of parameters are different * The return type of the method can be different * The modifiers of the method can be different * The main method can also be overloaded The following example demonstrates how to overload the `info` method of the `MyClass` class: ## MainClass.java File ```java class MyClass{ int height; MyClass(){ System.out.println("Parameterless Constructor"); height = 4; } MyClass(int i){ System.out.println("House height is " + i + " meters"); height = i; } void info(){ System.out.println("House height is " + height + " meters"); } void info(String s){ System.out.println(s + ": House height is " + height + " meters"); } } public class MainClass{ public static void main(String[]args){ MyClass t = new MyClass(3); t.info(); t.info("Overloaded Method"); //Overloaded Constructor new MyClass(); } } The output of the above code is: House height is 3 meters House height is 3 meters Overloaded Method: House height is 3 meters Parameterless Constructor [![Image 4: Java Example](#) Java Example](#)
← Python Bubble SortPython Quicksort β†’