What is polymorphism in java with example
polymorphism means one object can take multiple forms. lets explain this using a method poly() in java class.
steps:
- create a package polymorphismPkg in eclipse
- create a class polymorphismClass in eclipse
- now write below code in the class polymorphismClass
package polymorphismPkg;
public class polymorphismClass {
public static void main(String[] args) {
polymorphismClass obj1 = new polymorphismClass();
obj1.poly(1, 2);
obj1.poly(1, 2, 3);
obj1.poly(1, 22.11);
}
public static void poly(int a, int b)
{
System.out.println(“this is first method for polymorphism”);
}
public static void poly(int n, int b, int c)
{
System.out.println(“this is like first method but number of argument is different in signature “);
}
public static void poly(int n, double b)
{
System.out.println(“this is like first method but argument order is different in the signature “);
}
}