Question
Explain the outputs (or error) for the following test program. 1 public class TestAnimal { 2 public static void main(String[] args) { 3 // Using
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 }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started