Write a Java program to demonstrate dynamic method dispatch
This Java program illustrates the use of polymorphism and inheritance in object-oriented programming. It defines a hierarchy of classes, including a parent class Animal and two child classes Dog and Cat. The Animal class has a sound method, and the child classes override this method to provide their specific sound behaviors. The main method creates an array of Animal objects and demonstrates polymorphic behavior by calling the sound method on each element of the array. Here's an explanation of the program:
- public class Main: This is the main class containing the main method, which serves as the entry point of the program.
- public static void main(String[] args): The main method is where the program execution starts. It creates an array of Animal objects and assigns instances of Dog, Cat, and Animal to the array elements.
- Animal[] animals = new Animal[3]: An array of Animal objects with a length of 3 is created. This array can hold objects of the Animal class and its subclasses (Dog and Cat).
- animals[0] = new Dog();: The first element of the animals array is assigned a new Dog object. This demonstrates polymorphism because a subclass object can be stored in a reference variable of its superclass.
- animals[1] = new Cat();: The second element of the animals array is assigned a new Cat object, again demonstrating polymorphism.
- animals[2] = new Animal();: The third element of the animals array is assigned a new Animal object.
- for (Animal animal : animals): This is a for-each loop that iterates through the animals array, where each element is treated as an Animal object.
- animal.sound(): Within the loop, the sound method is called on each element of the array. This demonstrates polymorphism, as the specific implementation of the sound method depends on the actual object type.
- class Animal: This is the parent class, which defines a sound method that prints "Animal makes a sound."
- void sound(): The sound method in the Animal class prints "Animal makes a sound."
- class Dog extends Animal: This is a child class that extends the Animal class. It overrides the sound method to print "Dog barks."
- @Override void sound(): The Dog class overrides the sound method defined in the Animal class to provide the specific behavior for a dog, which is to bark.
- class Cat extends Animal: This is another child class that extends the Animal class. It overrides the sound method to print "Cat meows."
- @Override void sound(): The Cat class overrides the sound method to provide the specific behavior for a cat, which is to meow.
When the program runs, it demonstrates polymorphism. The sound method is called on each element of the animals array, and the output depends on the actual type of the object:
- For the first element (a Dog object), "Dog barks" is printed.
- For the second element (a Cat object), "Cat meows" is printed.
- For the third element (a generic Animal object), "Animal makes a sound" is printed.
This demonstrates how you can use inheritance and polymorphism to create a hierarchy of related classes with shared behavior.
Source Code
public class Main
{
public static void main(String[] args)
{
Animal[] animals = new Animal[3];
animals[0] = new Dog();
animals[1] = new Cat();
animals[2] = new Animal();
for (Animal animal : animals)
{
animal.sound();
}
}
}
class Animal
{
void sound()
{
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal
{
@Override
void sound()
{
System.out.println("Dog barks");
}
}
class Cat extends Animal
{
@Override
void sound()
{
System.out.println("Cat meows");
}
}
Output
Dog barks
Cat meows
Animal makes a sound