Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! I\'m having trouble with this JAVA problem about abstract classes and interf

ID: 3770053 • Letter: H

Question

Hi! I'm having trouble with this JAVA problem about abstract classes and interfaces. They're asking me ti explain how the following class TestEdible works and to say what is the output of the program.

The program is:

public class TestEdible {
public static void main(String[] args) {
Object[] objects = {new Tiger(), new Chicken(), new Apple()};
for(int i=0; i< objects.length; i++){
if(objects[i] instanceof Edible)
System.out.println(((Edible)objects[i]).howToEat());
  
if(objects[i] instanceof Animal){
System.out.println(((Animal)objects[i]).sound());
}
}
}
}

=====================================================================================
public interface Edible{
/**Describe how to eat */
public abstract String howtoEat ();
}

=====================================================================================

abstract class Animal{
/**Return animal sound */
public abstract String sound();
}

class Chicken extends Animal implements Edible{
@Override
public String howToEat(){
return "Chicken: Fry it";
}
  
@override
public String sound(){
return "Chicken: Cock-A-Doodle-Doo";
}
}
class Tiger extends Animal{
@Override
public String sound(){
return "Tiger: RROOOAAR";
}
}
abstract class Fruit implements Edible{
//Data fields, constructors, and methods omitted here
}
class Apple extends Fruit{
@Override
public String howToEat(){
return "Apple: Make apple cider";
}
}
class Orange extends Fruit{
@Override
public String howToEat(){
return "Orange: Make Orange Juice";
}
}

I need to understand how each class interacts depending on whether it's extending or implementing. I would really really appreciate any help and will gladly rate ++! Thanks!

Explanation / Answer

1. Every Java program execution starts from main method. That means the program execution starts from main method of testedible class.

2. A class contains abstract methods called abstract class. Abstract method means a method without body.

3. For example in the above program Animal is a abstract class and sound is abstract method. Sound method does not has any body of the method.

4. Interfaces are created by using the keyword interface. Interfaces can be called by using implements keyword. In the above program Edible is an interface.

5. Program execution starts from main method. Objects is an array to store the objects of classes.

6. The array contains Tiger class object, Chicken class object and Apple class object.

7. In the for loop all the objects are called one by one. That isTiger class object, Chicken class object and Apple class object. It calls indirectly all the methods and variables of the methods.

8. In the loop first it calls Tiger class object and it calls the method sound. So it displays"Tiger: RROOOAAR";

9. Then it calls chicken class object and it calls the method howtoeat() and displays Chicken: Fry it"; then calls sound method in chicken class and it displays"Chicken: Cock-A-Doodle-Doo";

10. Finally it calls Apple class object. It calls method of apple class that is howtoeat() and returns"Apple: Make apple cider";.

Out Put of the program:

Tiger: RROOOARROOOAARAR

Chicken: Fry it

Chicken: Cock-A-Doodle-Doo

Apple: Make apple cider

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote