We want to try to mimic multiple inheritance given the restrictions Java does pl
ID: 3687619 • Letter: W
Question
We want to try to mimic multiple inheritance given the restrictions Java does place on us. The code for the Food interface and Animal class can be found below.
public abstract class Animal {
private double weight;
protected Animal (double weight) {
this weight=weight;
}
public abstract void animalNoise();
{
public interface Food {
public abstract void howToEat();
}
Try to mimic multiple inheritance by extending the Animal class and implementing the Food interface. Take note of the abstract methods. Remember, we must implement these in any inheriting classes.
public abstract class Animal {
private double weight;
protected Animal (double weight) {
this weight=weight;
}
public abstract void animalNoise();
{
public interface Food {
public abstract void howToEat();
}
Explanation / Answer
Animal.java
public abstract class Animal {
private double weight;
public Animal(double weight) {
this.weight = weight;
}
public abstract void animalNoise();
}
Fish..java
public class Fish extends Animal implements Food{
String sound, eating;
public Fish(double weight) {
super(weight);
}
@Override
public void animalNoise(){
System.out.println("Fish: " + sound);
}
@Override
public void howToEat(){
System.out.println("Fish: " + eating);
}
}
Food.java
public interface Food {
public abstract void howToEat();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.