2. Java (12 points) Given the following inheritance hierarchy, write the output
ID: 3601107 • Letter: 2
Question
2. Java (12 points) Given the following inheritance hierarchy, write the output generated by each of the statements in the main method that follows. Or write "Compile time error" if one would occur. public abstract class Animal f public void id public class jungle Cat mittens new Cat): System.out.println ("I am an animal."); Calico khali new Calico); Dog feedo - new Dogl a) mittens.id b) feedo.play(); c) feedo-groom() public class Cat extends Animal f public void groom) System.out.print("Prr) ) // end Cat c) khali.id() public class Dog extends Animal f public void play() System.out.printin"Ruff Bite"); 1 // end Dog public class Calico extends Cat List the methods available to khali. public void id) System.out.print("1 am a Calico and "; super.id): public void play) System.out.printin('meow Scratch": ) l/end CalicoExplanation / Answer
We will have a compiler error in all the options as we can only have one public class in a .java file and here we have all the classes declared as public. So it is not possible to compile all the .java file as the compiler has an ambiguity in determining the public class.
The given code cannot be compiled:-
public abstract class Animal
{
public void id()
{
System.out.println("I am an Animal.");
}
}
public class Cat extends Animal
{
public void groom()
{
System.out.println("Prr");
}
}
public class Dog extends Animal
{
public void play()
{
System.out.println("Ruff Bite");
}
}
public class Calico extends Cat
{
public void id()
{
System.out.println("I am a Calico");
super.id();
}
public void play()
{
System.out.println("meow Scratch");
}
}
public class jungle
{
Cat mittens= new Cat();
Calico khali= new Calico();
Dog feedo= new Dog();
public static void main(String[] args)
{
mittens.id();
}
}
So, all the given commands if executed will give a compiler error due to ambiguity in the compiler as there is more than one public classes.
This is all for the question.
Thank You for using Chegg...!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.