Java – Polymorphism

Polymorphism: The ability of a method to perform different tasks based on its parameters or the object it is called on. Types: Method Overloading and Method Overriding.
Example:
Overloading:

class MathOps {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
MathOps ops = new MathOps();
System.out.println(ops.add(5, 10));
System.out.println(ops.add(5.5, 10.5));

Overriding:

class Parent {
void show() { System.out.println("Parent's show()"); }
}
class Child extends Parent {
void show() { System.out.println("Child's show()"); }
}
Parent obj = new Child();
obj.show();

No images available.