Question

(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Please find the required solution:

<<Java Class>> GAnimal (default package) Animal) d sound():void <<Java Class»> <<Java Class»> c Diary (default package) GMamm

/*Please find the required solution*/

import java.util.ArrayList;

//test method to save animal objects to array list
public class AnimalTest {
    public static void main(String[] args) {
        // define ArrayList
        ArrayList<Animal> animals = new ArrayList<>();

        // add animals to ArrayList
        animals.add(new Chicken());
        animals.add(new Bear());
        animals.add(new Cow());
        animals.add(new Sheep());

        // iterate through animals list
        for(Animal animal:animals){
            // invoke sound method
            animal.sound();

            // invoke how to eat if edible
            if(animal instanceof Edible) {
                Edible edible = (Edible)animal;
                edible.howToEat();
            }
        }

    }
}

// interface edible
interface Edible {
    void howToEat();

    void sound();
}

// abstract class animal
abstract class Animal {
    public abstract void sound();
}

abstract class Mammal extends Animal {
}

abstract class Diary extends Animal implements Edible {
}

class Sheep extends Mammal implements Edible {

    @Override
    public void howToEat() {
        System.out.println("Sheep Eat");
    }

    @Override
    public void sound() {
        System.out.println("Sheep sound");
    }
}

class Bear extends Mammal {
    @Override
    public void sound() {
        System.out.println("Bear Sound");
    }
}

class Chicken extends Diary {

    @Override
    public void howToEat() {
        System.out.println("Chicken Eat");
    }
    @Override
    public void sound() {
        System.out.println("Chicken Sound");
    }
}

class Cow extends Diary {

    @Override
    public void howToEat() {
        System.out.println("Cow Eat");
    }
    @Override
    public void sound() {
        System.out.println("Cow Sound");
    }
}

sample output:

Chicken Sound Chicken Eat Bear Sound Cow Sound Cow Eat Sheep sound Sheep Eat

Add a comment
Know the answer?
Add Answer to:
(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT