Java
In this tutorial we will learn about method overloading in Java programming language.
In Java when more than one method share the same name but different parameter declaration then the methods are said to be overloaded and the process is called method overloading.
class NameOfTheClass {
access_modifier return_type methodName(list_of_params_1) {
// some code...
}
access_modifier return_type methodName(list_of_params_2) {
// some code...
}
}
Where, NameOfTheClass
is the name of the class and it have two methods sharing the same name methodName
but they have different parameter declaration.
In the following example we the Compute
class having area
method that is overloaded.
If we pass single argument to the method then we are computing the area of a square. And if we pass two arguments then we are computing the area of a rectangle.
class Compute {
// this method will compute the area of a square
public double area(double side) {
return side * side;
}
// this method will compute the area of a rectangle
public double area(double length, double breadth) {
return length * breadth;
}
}
class MethodOverloadingExample {
public static void main(String[] args) {
// create object
Compute obj = new Compute();
// variable
double area, side, length, breadth;
// find the area of a square
side = 12.5;
area = obj.area(side);
System.out.println("Area of square having side " + side + " is " + area);
// find the area of a rectangle
length = 10.6;
breadth = 20;
area = obj.area(length, breadth);
System.out.println("Area of rectangle having length " + length + " and breadth " + breadth + " is " + area);
}
}
Output:
$ javac MethodOverloadingExample.java
$ java MethodOverloadingExample
Area of square having side 12.5 is 156.25
Area of rectangle having length 10.6 and breadth 20.0 is 212.0
In the following example we are creating a Happy
class having isHappy
method that is overloaded.
The first method is:
public void isHappy(boolean happy) {
}
It takes boolean value as argument and prints an output message based on the argument value.
The second method is:
public boolean isHappy(int happinessLevel) {
}
This takes an integer argument and based on the value it returns a boolean value back to the calling method. We can then use the returned value and some suitable output message.
class Happy {
// this method will print the output based on boolean value passed
public void isHappy(boolean happy) {
if (happy == true) {
System.out.println("Person is happy :)");
} else {
System.out.println("Person is sad :(");
}
}
// this method will return boolean value
public boolean isHappy(int happinessLevel) {
if (happinessLevel > 5) {
return true;
} else {
return false;
}
}
}
class MethodOverloadingExample {
public static void main(String[] args) {
// create object
Happy person = new Happy();
// passing boolean value and printing result
System.out.println("Is person happy? ");
person.isHappy(true);
// passing integer value and getting back boolean value
int happinessLevel = 5;
boolean isPersonHappy = person.isHappy(happinessLevel);
// now using the isPersonHappy boolean value and calling the method
System.out.println("Happiness Level " + happinessLevel);
person.isHappy(isPersonHappy);
}
}
Output:
$ javac MethodOverloadingExample.java
$ java MethodOverloadingExample
Is person happy?
Person is happy :)
Happiness Level 5
Person is sad :(
ADVERTISEMENT