What is polymorphism
- The word polymorphism is derived from Greek and means "having multiple forms." Apart from computer programming, the idea of polymorphism occurs in other real-world areas, including biology, chemistry and drug development.
- Polymorphism is one of the most important concepts in OOP. It describes the ability of something to have or to be displayed in more than one formMany programming languages display or allow
- polymorphism, including Java, Ruby, C++, PHP and Python. In these languages, polymorphism enables class objects belonging to the same hierarchical tree to behave differently, even though they might have functions with the same name. In PHP, for example, polymorphism means that if B is a descendant of A and a function can accept A as a parameter, then it can also accept B as a parameter.
Key features of polymorphism:
- Multiple Behaviors: The same method can behave differently depending on the object that calls this method.
- Method Overriding: A child class can redefine a method of its parent class.
- Method Overloading: We can define multiple methods with the same name but different parameters.
- Runtime Decision: At runtime, Java determines which method to call depending on the object's actual class.[TBD]
Why polymorphism
- Polymorphism is a robust feature of OOP. It increases the reusability, flexibility and extensibility of code. Take the above example for instance:
- Reusability: the teach() method can be re-used for different kinds of objects as long as they are sub types of the Animal interface.
- Flexibility: the actual object can be determined at runtime which allows the code run more flexibly.
- Extensibility: when we want to add a new kind of Animal, e.g. Snake, we just pass an object of Snake into the teach() method without any modification.
Type of polymorphism
- You can perform Polymorphism in Java via two different methods:
- Method Overloading
- Method Overriding
What is Method Overloading in Java?
- Method overloading is the process that can create multiple methods of the same name in the same class, and all the methods work in different ways. Method overloading occurs when there is more than one method of the same name in the class. example code:
class Shapes {
public void area() {
System.out.println("Find area ");
}
public void area(int r) {
System.out.println("Circle area = "+3.14*r*r);
}
public void area(double b, double h) {
System.out.println("Triangle area="+0.5*b*h);
}
public void area(int l, int b) {
System.out.println("Rectangle area="+l*b);
}
}
class Main {
public static void main(String[] args) {
Shapes myShape = new Shapes(); // Create a Shapes object
myShape.area();
myShape.area(5);
myShape.area(6.0,1.2);
myShape.area(6,2);
}
}
Output:
Find area
Circle area = 78.5
Triangle area=3.60
Rectangle area=12
What is Method Overriding in Java?
- Method overriding is the process when the subclass or a child class has the same method as declared in the parent class. example code:
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is moving");}
}
//Creating a child class
class Car2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("car is running safely");}
public static void main(String args[]){
Car2 obj = new Car2();//creating object
obj.run();//calling method
}
}
Output:
Car is running safely
Reference
https://www.techtarget.com/whatis/definition/polymorphism
https://www.geeksforgeeks.org/java/polymorphism-in-java/
https://www.codejava.net/java-core/the-java-language/what-is-polymorphism-in-java-the-what-how-and-why
https://www.mygreatlearning.com/blog/polymorphism-in-java/
Top comments (0)