Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

abstract public class Animal { abstract public void greeting(); } public class Cat extends Animal { @Override public void greeting() { System.out.println(Meow!); } } public

abstract public class Animal { abstract public void greeting(); }

public class Cat extends Animal { @Override public void greeting() { System.out.println("Meow!"); } }

public class Dog extends Animal { @Override public void greeting() { System.out.println("Woof!"); } public void greeting(Dog another) { System.out.println("Woooooooooof!"); } }

public class BigDog extends Dog { @Override public void greeting() { System.out.println("Woow!"); } @Override public void greeting(Dog another) { System.out.println("Woooooowwwww!"); } }

Explain the outputs (or error) for the following test program. Make sure to include what line th error is on, a discription, and if it is an output error or error.

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions