Sobes.tech
Junior — Middle

How can you place three objects of different classes into one array using polymorphism capabilities?

sobes.tech AI

Answer from AI

In Java, to place objects of different classes into a single array, you can use polymorphism by declaring an array of a common superclass or interface that these classes implement.

For example, if there are classes Cat, Dog, and Bird, all inheriting from the class Animal, you can create an array of Animal[] and put objects of all three classes into it:

Animal[] animals = new Animal[3];
animals[0] = new Cat();
animals[1] = new Dog();
animals[2] = new Bird();

This is possible because all objects are instances of subclasses of Animal, and an array of type Animal[] can contain any objects that inherit from Animal.