*what is polymorphism in java *
- Polymorphism in Java is a core concept of Object-Oriented Programming (OOP) that allows objects to take on "many forms,"derived from the Greek words "poly" (many) and "morph" (forms).
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.
Types of Polymorphism in Java
In Java Polymorphism is mainly divided into two types:
- Compile-Time Polymorphism (Static)
- Runtime Polymorphism (Dynamic)
Compile-time Polymorphism (Static Polymorphism):
- This type of polymorphism is resolved during the compilation phase.
- It is achieved primarily through Method Overloading.
- Method Overloading: involves defining multiple methods within the same class that share the same name but have different parameters (different number of parameters, different types of parameters, or a combination of both). The compiler determines which overloaded method to invoke based on the arguments provided during the method call.
class MathOperations {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two double values
double add(double a, double b) {
return a + b;
}
}
public class CompileTimePolymorphismExample {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println("Sum of two integers: " + math.add(5, 10));
System.out.println("Sum of three integers: " + math.add(5, 10, 15));
System.out.println("Sum of two doubles: " + math.add(5.5, 10.5));
}
}
Runtime Polymorphism (Dynamic Polymorphism):
- This type of polymorphism is resolved during the execution (runtime) of the program.
- It is achieved primarily through Method Overriding.
- Method Overriding: occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. When an overridden method is called through a reference variable of the superclass, the actual method invoked is determined at runtime based on the actual object type being referred to by the reference variable (Dynamic Method Dispatch). This often involves Upcasting, where a subclass object is referred to by a superclass reference.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class RuntimePolymorphismExample {
public static void main(String[] args) {
Animal myAnimal;
myAnimal = new Dog();
myAnimal.sound(); // Outputs: Dog barks
myAnimal = new Cat();
myAnimal.sound(); // Outputs: Cat meows
}
}
Top comments (0)