Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain the outputs (or error) for the following test program. 1 public class Te

ID: 3718009 • Letter: E

Question

Explain the outputs (or error) for the following test program.

1 public class TestAnimal {
2   public static void main(String[] args) {
3      // Using the subclasses
4      Cat cat1 = new Cat();
5      cat1.greeting();
6      Dog dog1 = new Dog();
7      dog1.greeting();
8      BigDog bigDog1 = new BigDog();
9      bigDog1.greeting();
10       
11      // Using Polymorphism
12      Animal animal1 = new Cat();
13      animal1.greeting();
14      Animal animal2 = new Dog();
15      animal2.greeting();
16      Animal animal3 = new BigDog();
17      animal3.greeting();
18      Animal animal4 = new Animal();
19      
20      // Downcast
21      Dog dog2 = (Dog)animal2;
22      BigDog bigDog2 = (BigDog)animal3;
23      Dog dog3 = (Dog)animal3;
24      Cat cat2 = (Cat)animal2;
25      dog3.greeting(dog2);
26      dog2.greeting(bigDog2);
27      bigDog2.greeting(dog2);
28   }
29 }

Explanation / Answer

4      Cat cat1 = new Cat(); // create a new Cat Class object
5      cat1.greeting(); // print the message in greeting() method that is overriden in Cat class
6      Dog dog1 = new Dog(); // create a new Dog object
7      dog1.greeting(); // print the message in greeting() method that is overriden in Dog class
8      BigDog bigDog1 = new BigDog(); // create a new BigDog object
9      bigDog1.greeting(); // print the message in greeting() method that is overriden in BigDog class.

11      // Using Polymorphism
12      Animal animal1 = new Cat(); // Parent class reference variable can store the object of its child
13      animal1.greeting(); // call the greeting method of Cat class since the object referred by animal1 is Cat object
14      Animal animal2 = new Dog();  // Parent class reference variable can store the object of its child
15      animal2.greeting(); // call the greeting method of Cat class since the object referred by animal2 is Dog object
16      Animal animal3 = new BigDog(); // Parent class reference variable can store the object of its child
17      animal3.greeting(); // call the greeting method of Cat class since the object referred by animal3 is BigDog object
18      Animal animal4 = new Animal(); // create a new Object of Parent class Animal.

// Downcast
21      Dog dog2 = (Dog)animal2; // Explicitly type casting animal2 into Dog object
22      BigDog bigDog2 = (BigDog)animal3; // Explicitly type casting animal3 into BigDog object
23      Dog dog3 = (Dog)animal3; // Explicitly type casting BigDog object into Dog (ie. Parent) object
24      Cat cat2 = (Cat)animal2; // Error as we are type casting Dog object into Cat object which is invalid
25      dog3.greeting(dog2);
26      dog2.greeting(bigDog2);
27      bigDog2.greeting(dog2);

Please let me know in case of any clarifications required. Thanks!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote