7.3k views
5 votes
The following questions are based on the Edible interface and the classes defined in java Ch. 13 (slides 16-19). For each question, answer if the code can compile, can run. If not, give a reason. If it runs, give the output.

a. Edible x = new Tiger();
b. Edible x = new Chicken();
System.out.println(x.sound());
c. Edible x = new Chicken();
System.out.println((Animal)x.sound());
d. Edible x = new Chicken();
System.out.println(((Animal)x).sound());

1 Answer

5 votes

Final answer:

The code can compile and run, except for the third line it will throw a ClassCastException at runtime. Casting x to Animal allows the use of the Animal reference to access methods specific to the Animal class.

Step-by-step explanation:

a. The code can compile and run. The output will depend on the implementation of the Tiger class and the method called. Since Tiger is a type of Edible, it is valid to assign a Tiger object to an Edible variable.

b. The code can compile and run. The output will depend on the implementation of the Chicken class and the method called. Since Chicken is a type of Edible, it is valid to assign a Chicken object to an Edible variable.

c. The code can compile, but it will throw a ClassCastException at runtime. The sound() method of the Chicken class is called through the variable x, which is of type Edible. Casting x to Animal does not change the underlying object, so when the sound() method is called, it still refers to the implementation in the Chicken class. However, since the sound() method returns a String, the cast to Animal will throw a ClassCastException.

d. The code can compile. It will run without throwing any exceptions, but the output will depend on the implementation of the Chicken class and the method called. Casting x to Animal does not change the underlying object, but it allows the use of the Animal reference to access methods specific to the Animal class.

User Santo Boldizar
by
7.7k points