Types of Polymorphism in Java
Polymorphism is an important concept in object-oriented programming, which allows objects to take on multiple forms depending on the context in which they are used. In Java, there are three main types of polymorphism: method overloading, method overriding, and runtime polymorphism. In this article, we will discuss each of these types of polymorphism in detail.
- Method Overloading: Method overloading is a type of polymorphism in which a class has multiple methods with the same name, but different parameters. The Java compiler distinguishes between these methods based on their parameter types and the number of parameters. For example, consider the following code snippet:
public class MyClass {
public void print(int x) {
System.out.println(“Printing an integer: “ + x);
}
public void print(String str) {
System.out.println(“Printing a string: “ + str);
}
public void print(double d) {
System.out.println(“Printing a double: “ + d);
}
}
In this example, the MyClass
class has three methods named print()
, each with a different parameter type. When we call the print()
method with different arguments, the Java compiler automatically selects the appropriate method based on the type of argument passed.
2. Method Overriding Method: overriding is a type of polymorphism in which a subclass provides its own implementation of a method that is already defined in its superclass. When we call the method on the subclass object, the method defined in the subclass is executed instead of the method defined in the superclass. For example:
public class Animal {
public void speak() {
System.out.println(“The animal speaks”);
}
}
public class Dog extends Animal {
@Override
public void speak() {
System.out.println(“The dog barks”);
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
Dog dog = new Dog();
animal.speak(); // The animal speaks
dog.speak(); // The dog barks
}
}
In this example, the Animal
class defines a speak()
method, which is overridden by the Dog
class to provide a different implementation of the method.
3. Runtime Polymorphism: Runtime polymorphism is a type of polymorphism in which the method to be executed is determined at runtime, based on the actual type of the object being used. This is also known as dynamic method dispatch. For example:
public class Shape {
public void draw() {
System.out.println(“Drawing a shape”);
}
}
public class Rectangle extends Shape {
@Override
public void draw() {
System.out.println(“Drawing a rectangle”);
}
}
public class Circle extends Shape {
@Override
public void draw() {
System.out.println(“Drawing a circle”);
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Rectangle();
Shape shape2 = new Circle();
shape1.draw(); // Drawing a rectangle
shape2.draw(); // Drawing a circle
}
}
In this example, the Shape
class defines a draw()
method, which is overridden by the Rectangle
and Circle
classes to provide different implementations of the method. The Main
class creates objects of these classes and assigns them to variables of the Shape
type. When we call the draw()
method on these objects, the appropriate implementation of the method is executed based on the actual type of the object.
Make 1000 of $ through this websites
https://medium.com/@rushikeshwagh401/you-can-earn-up-to-10-000-through-this-website-a3e7c60bc731
In conclusion, polymorphism is an important concept in Java, and there are three main types of polymorphism: method