Q:

Create an interface Animal with methods - animalSound() - run() - type() and Create 3 classes Tiger, Cow, and Goat. All these 3 classes implement the Animal interface

1

Create an interface Animal with methods
- animalSound() display animal sound in implemented class
- run() display speed of the animal in implemented class
- type() display type of animal either Herbivores or Carnivores (Herbivores are animals that eat only plants. Carnivores are animals that eat only meat)

Create 3 classes Tiger, Cow, and Goat. All these 3 classes implement the Animal interface.

Create 3 instances of type Animal and assign Tiger, Cow, and Goat reference.

Invoke all the 3 interface methods for each animal instances.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

interface Animal:

public interface Animal {
  void animalSound();
  void run();
  void type();  
}

 

class Tiger:

public class Tiger implements Animal{
  public void animalSound() {
    System.out.println("tiger sound...");
  }
  public void run() {
    System.out.println("120km/h");
  }
  public void type() {
    System.out.println("Carnivores");
  }
}

 

class Cow:

public class Cow implements Animal{
    public void animalSound() {
    System.out.println("cow sound...");
  }
  public void run() {
    System.out.println("5km/h");
  }
  public void type() {
    System.out.println("Herbivores");
  }  
}

class Goat:

public class Goat implements Animal{
   public void animalSound() {
    System.out.println("goat sound...");
  }
  public void run() {
    System.out.println("15km/h");
  }
  public void type() {
    System.out.println("Herbivores");
  }   
}

class Main:

public class Main {
    
public static void main(String[] args) {
 Tiger t=new Tiger();
Cow c=new Cow();
Goat g=new Goat();

t.animalSound();
t.run();
t.type();
c.animalSound();
c.run();
c.type();
g.animalSound();
g.run();
g.type();
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now