java
method
programming
oop
Polymorphism is one of the oops pillars in java. It has two types. They are,
1.Compile time polymorphism (or) Method Overloading
2.Run time polymorphism (or) Method Overriding
1.Method Overloading:
Method Overloading allows multiple methods with the same name but different number and types of arguments within a class.
Method Overloading is very important to naming convertion.
Example:
public class SuperMarket
{
static String shopname = "Kanchi Super Market";
String product_name;
int price;
public static void main(String[] args)
{
SuperMarket product = new SuperMarket();
product.buy(10);
product.buy(5,50);
product.buy(10.5f, 10.3f);
product.buy(100.5d);
}
void buy(int no)
{
System.out.println("buy one args" +"=" +no);
}
void buy(int no1, int no2)
{
System.out.println("buy two args" +"=" +no1+" "+no2);
}
void buy(float no3, float no5)
{
System.out.println("buy two float args" +"=" +no3+" "+no5);
}
void buy(double no4)
{
System.out.println("buy one double args"+"=" +no4);
}
}
Output:
buy one args=10
buy two args=5 50
buy two float args=10.5 10.3
buy one double args=100.5
Top comments (0)