0% found this document useful (0 votes)
1 views3 pages

Method Overloading in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Method Overloading in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Method Overloading in Java

Method Overloading is a feature in Java that allows a class to have multiple methods with the same name
but different parameter lists.

The difference can be in:

 Number of parameters
 Type of parameters
 Order of parameters

� It is also called Compile-Time Polymorphism.

Why Method Overloading?

 Improves code readability


 Reduces method name confusion
 Makes programs easy to understand and use

Rules of Method Overloading

1. Method name must be same


2. Parameter list must be different
3. Return type alone cannot differentiate methods
4. Can be done in the same class

Example 1: Overloading by Number of Parameters

class Demo {

void add(int a, int b) {


[Link](a + b);
}

void add(int a, int b, int c) {


[Link](a + b + c);
}

public static void main(String[] args) {


Demo obj = new Demo();
[Link](10, 20);
[Link](10, 20, 30);
}
}
Example 2: Overloading by Type of Parameters
class Demo {

void add(int a, int b) {


[Link](a + b);
}

void add(double a, double b) {


[Link](a + b);
}

public static void main(String[] args) {


Demo obj = new Demo();
[Link](10, 20);
[Link](10.5, 20.5)}}
Example 3: Overloading by Order of Parameters
class Demo {

void show(int a, char b) {


[Link](a + " " + b);
}

void show(char a, int b) {


[Link](a + " " + b);
}

public static void main(String[] args) {


Demo obj = new Demo();
[Link](10, 'A');
[Link]('B', 20);
}
}

Assignment-6 Question-10
Write a java program to calculate the area of triangle, square, circle, rectangle by using method
overloading.
Sample run1:
=== AREA CALCULATOR (Method Overloading) ===
1. Area of Square
2. Area of Rectangle
3. Area of Circle
4. Area of Triangle
5. Exit Enter your choice: 1
Enter side: 5
Area of Square: 25
Program:
package Assign6;

import [Link];

public class Q10 {

public static double area(int side) {


return side*side;
}
public static double area(int length,int width) {
return length*width;
}
public static double area(double radius) {
return [Link]*radius*radius;
}
public static double area(double base,double height) {
return 0.5*base*height;
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
int ch;
do {
[Link]("=== AREA CALCULATOR (Method overloading) ===");
[Link]("1. Area of square");
[Link]("2. Area of rectangle");
[Link]("3. Area of circle");
[Link]("4. Area of triangle");
[Link]("5. Exit");
[Link]("Enter your choice:");
ch=[Link]();
switch(ch) {
case 1:
[Link]("enter side:");
int side=[Link]();
[Link]("area of square: "+area(side));
break;

case 2:
[Link]("enter length and width:");
int length=[Link]();
int width=[Link]();
[Link]("area of rectangle: "+area(length,width));
break;

case 3:
[Link]("enter radius:");
double radius=[Link]();
[Link]("area of circle: "+area(radius));
break;

case 4:
[Link]("enter base and height:");
double base=[Link]();
double height=[Link]();
[Link]("area of triangle: "+area(base,height));
break;

case 5:
[Link]("Exiting...");
break;
default:
[Link]("invalid choice");

}
}while(ch!=5);

You might also like