Polymorphism in Java
📍 Polymorphism allows a single interface to be used for different types of actions.
📍 It is of two types:
Method Overloading (Compile-time Polymorphism)
Method Overriding (Runtime Polymorphism)
Method Overloading (Same method name, different parameters)
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Method Overriding (Same method in parent and child class)
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
🔗 More Java Resources: https://whatsapp.com/channel/0029VamdH5mHAdNMHMSBwg1s
Post #1101
7.19K
- ❤ 16