in java 1. Create an interface Edible that has one method howToEat that returns
ID: 3821005 • Letter: I
Question
in java
1. Create an interface Edible that has one method howToEat that returns a String
2. Create an abstract Animal class that has one abstract method sound that returns a String
3. Create an abstract Fruit class.
4. The Fruit class will implement the Edible interface (no code should be in this class)
5. Create an Apple class that inherits from Fruit
6. Create an Orange class that inherits from Fruit
7. Create a Lion class that inherits from Animal
8. Create a Chicken class that inherits from Animal and implements the Edible interface Draw a UML diagram to show the relationship between the classes and interface.
Testing:
1. Create and driver to test the classes
a. Create an array of type Object that contain each class
b. Create an array of type Edible to test all Edible object
Discussion:
In Testing 1.a there is a problem. How do you know what the object type is to test the howToEat or sound method.
You will need to do this: A instanceof B returns true if A has a relationship with B through inheritance or an interface.
Explanation / Answer
Hi,
Please see below all the classes.
Thanks,
Edible.java
public interface Edible {
public String howToEat ();
}
Animal.java
public abstract class Animal {
public abstract String sound();
}
Fruit.java
public abstract class Fruit implements Edible {
}
Apple.java
public class Apple extends Fruit {
public String howToEat() {
System.out.println("This is howtoEat method of Apple Class");
return "";
}
}
Orange.java
public class Orange extends Fruit {
public String howToEat() {
System.out.println("This is howtoEat method of Orange Class");
return "";
}
}
Lion.java
public class Lion extends Animal {
public String sound() {
System.out.println("This is sound method of Lion Class");
return "";
}
}
Chicken.java
public class Chicken extends Animal implements Edible {
public String howToEat() {
System.out.println("This is howToEat method of Chicken Class");
return "";
}
public String sound() {
System.out.println("This is sound method of Chicken Class");
return "";
}
}
InterfaceTester.java
public class InterfaceTester {
public static void main(String [] args){
//Creating object of all types
Apple apple = new Apple();
Orange orange = new Orange();
Lion lion = new Lion();
Chicken chicken= new Chicken();
//Adding all objects to Object array
Object [] objArray = new Object[]{apple,orange,lion,chicken};
Edible [] edibleArray = new Edible[4]; //Edible array
int counter =0;
//Looping throug each object
for(Object object : objArray){
if(object instanceof Edible){ //Checking if current object is an instance of Edible
edibleArray[counter] = (Edible)object;
edibleArray[counter].howToEat(); //Calling howToEat on Edible
counter ++;
}
if(object instanceof Animal){ //Checking if current object is an instance of Animal
Animal animal= (Animal)object;
animal.sound(); //Calling sound on Animal
}
}
}
}
SAmple output:
This is howtoEat method of Apple Class
This is howtoEat method of Orange Class
This is sound method of Lion Class
This is howToEat method of Chicken Class
This is sound method of Chicken Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.