Explore what CodeHS has to offer for districts, schools, and teachers.
Click on one of our programs below to get started coding in the sandbox!
View All
The Animal, Cat, and Lion classes are defined as follows:
Animal
Cat
Lion
public class Animal { private String name; public Animal(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String toString() { return "I am a " + name; } } public class Cat extends Animal { public Cat() { super("Cat"); } public String speak() { return "meow!"; } @Override public String toString() { return super.toString() + ", " + speak(); } } public class Lion extends Cat { public Lion() { super(); this.setName("Lion"); } @Override public String speak() { return "rawr!"; } }
What will the following program output?
Lion lion = new Lion(); System.out.println(lion.toString());
This program will fail to compile because Lion does not have a toString() method
toString()
“I am a Cat, meow!”
“I am a Lion, meow!”
“I am a Lion, rawr!”
“I am a Lion”