Login or Create an Account to view the mark scheme, comment, and add to a test
An Object Oriented program simulating the behavior of different animals has each type of animal class inherit from a parent Animal class as defined below
[3]// Java code in Animal.java
public class Animal {
private String diet;
// constructor
public Animal(String d) {
this.diet = d;
}
// accessor method
public String getDiet() {
return diet;
}
public void talk() {
System.out.println("Hello, I eat " + getDiet());
}
}
// Java code in Dog.java
public class Dog extends Animal {
private String sound;
// constructor
public Dog(String n, String d) {
<< insert code here >>
}
// accessor method
public String getSound() {
<< insert code here >>
}
public void talk() {
System.out.println(getSound() + ", I eat " + getDiet());
}
}
class Main {
public static void main(String[] args) {
Dog myPet = new Dog("Woof", "biscuits");
myPet.talk();
}
}
Write a constructor function and accessor method to complete the Dog class such that the main function yields the following:
"Woof, I eat biscuits"
Short Answer3 MarksPremium
1 Use8 Views1 Like