Dynamic method dispatch, also known as runtime polymorphism, is a feature in object-oriented programming that allows a program to determine which implementation of a method to invoke at runtime, based on the actual type of the object being referenced. It enables method calls to be resolved dynamically at runtime rather than being determined at compile-time.
Dynamic method dispatch is closely related to inheritance and overriding. When a subclass overrides a method declared in its superclass, the subclass provides its own implementation of that method. This means that objects of the subclass can be treated as objects of the superclass, and the overridden method can be invoked through a reference of the superclass type. However, the specific implementation of the method that is executed is determined by the actual type of the object being referenced.
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Animal();
Animal animal2 = new Dog();
Animal animal3 = new Cat();
animal1.makeSound(); // Output: Animal makes a sound
animal2.makeSound(); // Output: Dog barks
animal3.makeSound(); // Output: Cat meows
}
}
In this example, we have a superclass Animal
and two subclasses Dog
and Cat
. Each class has its own implementation of the makeSound()
method. In the main()
method, we create objects of the different classes and assign them to variables of the type Animal
.
When the makeSound()
method is called on animal1
, which references an Animal
object, the implementation from the Animal
class is invoked. However, when the same method is called on animal2
and animal3
, which reference Dog
and Cat
objects, respectively, the overridden implementations from the respective subclasses are invoked. This is dynamic method dispatch in action, where the specific method implementation is determined by the actual type of the object being referenced, rather than the declared type of the variable.
Dynamic method dispatch allows for more flexible and polymorphic behavior in object-oriented programming, enabling objects of different types to be treated uniformly through their common superclass or interface, while still invoking their specific behavior at runtime.
In Java, upcasting and downcasting are related to the concepts of inheritance and polymorphism, and they involve the manipulation of object references.
- Upcasting: Upcasting refers to the process of assigning a reference of a subclass to a reference variable of its superclass. It is an implicit conversion that happens automatically. Upcasting is safe because a subclass object inherently has all the properties and behaviors of its superclass.
- Downcasting: Downcasting is the opposite of upcasting. It involves casting a reference of a superclass to a reference variable of its subclass. Since a superclass reference can refer to a subclass object, downcasting is required to access the specific members (methods or fields) of the subclass.
However, downcasting is not automatically done and requires an explicit cast. It is important to note that downcasting can only be performed when the actual object being referenced is of the target subclass type. Otherwise, a ClassCastException
will be thrown at runtime.
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.