What is polymorphism?
- Polymorphism is a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times
Why do we use polymorphism in Java?
*Polymorphism is used to perform a single action in different ways. It allows for the implementation of dynamic method dispatch, where the method to be executed is determined at runtime. This is beneficial for implementing a cleaner and more maintainable codebase.
Polymorphism in Java allows objects to take on many forms. It is a core concept of object-oriented programming (OOP) that enables objects of different classes to be treated as objects of a common parent class. This provides flexibility and the ability to define methods in multiple forms. Polymorphism is achieved through method overriding and method overloading.
Types of Polymorphism in Java
*In Java Polymorphism is mainly divided into two types:
1. Compile-Time Polymorphism (Static)
2.Runtime Polymorphism (Dynamic)
*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
Real-Life Illustration of Polymorphism
*Consider a person who plays different roles in life, like a father, a husband, and an employee. Each of these roles defines different behaviors of the person depending on the object calling it.
Example: Different Roles of a Person
// Base class Person
class Person {
// Method that displays the
// role of a person
void role() {
System.out.println("I am a person.");
}
}
// Derived class Father that
// overrides the role method
class Father extends Person {
// Overridden method to show
// the role of a father
@Override
void role() {
System.out.println("I am a father.");
}
}
public class Main {
public static void main(String[] args) {
// Creating a reference of type Person
// but initializing it with Father class object
Person p = new Father();
// Calling the role method. It calls the
// overridden version in Father class
p.role();
}
}
Output : I am a father.
Explanation:
*In the above example, the Person class has a method role() that prints a general message. The Father class overrides role() to print a specific message. The reference of type Person is used to point to an object of type Father, demonstrating polymorphism at runtime. The overridden method in Father is invoked when role() is called.
1.Compile-Time Polymorphism in Java
* It is also known as static polymorphism. This type of polymorphism is achieved by function overloading.
Method Overloading
*Method overloading in Java refers to the ability to define multiple methods in the same class with the same name but with different parameters.
* Java compiler differentiates these methods based on the number of parameters or the data types of parameters
Example: Method overloading by changing the number of arguments
// Method overloading By using
// Different Types of Arguments
// Class 1
// Helper class
class Helper {
// Method with 2 integer parameters
static int Multiply(int a, int b)
{
// Returns product of integer numbers
return a * b;
}
// Method 2
// With same name but with 2 double parameters
static double Multiply(double a, double b)
{
// Returns product of double numbers
return a * b;
}
}
// Class 2
// Main class
class Geeks
{
// Main driver method
public static void main(String[] args) {
// Calling method by passing
// input as in arguments
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
}
}
Output
8
34.65
Explanation:
- The Multiply method is overloaded with different parameter types. The compiler picks the correct method during compile time based on the arguments.
- Runtime Polymorphism: Runtime Polymorphism in Java known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method *Method Overriding** *Method overriding in Java means when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass. Method overriding allows a subclass to modify or extend the behavior of an existing method in the parent class. This enables dynamic method dispatch, where the method that gets executed is determined at runtime based on the object's actual type.
Example:
* This program demonstrates method overriding in Java, where the Print() method is redefined in the subclasses (subclass1 and subclass2) to provide specific implementations.
// Java Program for Method Overriding
// Class 1
// Helper class
class Parent {
// Method of parent class
void Print() {
System.out.println("parent class");
}
}
// Class 2
// Helper class
class subclass1 extends Parent {
// Method
void Print() {
System.out.println("subclass1");
}
}
// Class 3
// Helper class
class subclass2 extends Parent {
// Method
void Print() {
System.out.println("subclass2");
}
}
// Class 4
// Main class
class Geeks {
// Main driver method
public static void main(String[] args) {
// Creating object of class 1
Parent a;
// Now we will be calling print methods
// inside main() method
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}
Output
subclass1
subclass2
Explanation
* In the above example, when an object of a child class is created, then the method inside the child class is called. This is because the method in the parent class is overridden by the child class. This method has more priority than the parent method inside the child class. So, the body inside the child class is executed.
Referred links :
Top comments (0)