What is an example of polymorphism in Java?
What is an example of polymorphism in Java?
I'm happy to respond in English for you!
Polymorphism in Java is the ability of an object to take on multiple forms. The most common type of polymorphism is method overriding, where a subclass provides a different implementation of a method that already exists in its superclass.
Here's an example:
// Animal class - superclass
class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}
// Dog class - subclass
class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
// Cat class - subclass
class Cat extends Animal {
@Override
public void sound() {
System.out.println("The cat meows");
}
}
public class Main {
public static void main(String[] args) {
// Create an array of Animals
Animal[] animals = new Animal[3];
animals[0] = new Dog();
animals[1] = new Cat();
animals[2] = new Animal(); // base class
// Loop through the array and call sound()
for (Animal animal : animals) {
animal.sound();
}
}
}
In this example, we have an Animal
superclass with a method called sound()
. We also have two subclasses: Dog
and Cat
, which both override the sound()
method to provide their own implementation.
In the Main
class, we create an array of Animal
s and fill it with objects of type Dog
, Cat
, and the base Animal
class. Then, we loop through the array and call the sound()
method on each object.
When we run this code, the output will be:
The dog barks
The cat meows
The animal makes a sound
This demonstrates polymorphism because an object of type Dog
is treated as if it's an Animal
, even though they are different classes. The correct sound()
method is called based on the actual class of the object, which is determined at runtime.
This example also illustrates the concept of dynamic method dispatch, where the method to be called is determined at runtime rather than compile time. This is a key feature of polymorphism in Java and allows for more flexibility and code reuse when working with objects of different classes.
What is the difference between static and runtime polymorphism in Java?
In object-oriented programming (OOP), polymorphism refers to the ability of an object to take on multiple forms. There are two main types of polymorphism: static polymorphism and runtime polymorphism.
Static Polymorphism
Static polymorphism, also known as compile-time polymorphism or early binding, is the process by which the compiler resolves a method's invocation based on its static type at compile time. In other words, the compiler decides which method to call based on the declared types of variables and expressions. This type of polymorphism is achieved through method overloading (also known as compile-time dispatch) or method overriding.
Method Overloading:
In Java, method overloading allows multiple methods with the same name but different parameter lists to be defined in a class. The compiler decides which overloaded method to call based on the number and types of arguments passed. For example:
public class Calculator {
public int add(int x, int y) { return x + y; }
public double add(double x, double y) { return x + y; }
}
In this case, if you create a Calculator
object and call the add()
method with two int
s, the compiler will invoke the first overloaded method. If you pass two double
s, it will invoke the second overloaded method.
Method Overriding:
Method overriding is another type of static polymorphism that allows a subclass to provide a specific implementation for a method that is already defined in its superclass. The compiler resolves the method invocation based on the declared types and relationships between classes. For example:
public class Animal {
public void makeSound() { System.out.println("The animal makes a sound."); }
}
public class Dog extends Animal {
@Override
public void makeSound() { System.out.println("Woof!"); }
}
In this case, if you have an Animal
object and call the makeSound()
method, the compiler will invoke the method defined in the Animal
class. If you create a Dog
object (which is a subclass of Animal
) and call the same makeSound()
method, the compiler will override the method with the implementation defined in the Dog
class.
Runtime Polymorphism
Runtime polymorphism, also known as dynamic dispatch or late binding, is the process by which the JVM decides which method to call based on the actual type of an object at runtime. This type of polymorphism is achieved through method overriding (as shown above) and dynamic method invocation using interfaces or abstract classes.
One key difference between static and runtime polymorphism is when the compiler or JVM resolves the method invocation:
Static polymorphism: The compiler resolves the method invocation based on declared types at compile time. Runtime polymorphism: The JVM resolves the method invocation based on the actual type of an object at runtime.Another significant difference is the flexibility and flexibility provided by runtime polymorphism. With static polymorphism, the compiler decides which method to call based on the declared types, whereas with runtime polymorphism, the JVM can decide which method to call based on the actual type of an object, even if that object was created dynamically at runtime.
In summary, while both static and runtime polymorphism are forms of polymorphism in Java, they differ significantly in terms of when the compiler or JVM resolves method invocations. Static polymorphism is about compile-time resolution based on declared types, whereas runtime polymorphism is about dynamic dispatch based on actual object types at runtime.