Question: Given the parent abstract class Animal, create child classes Dog, Cat and Pig that implement the methods defined in the abstract class. In addition, create

Given the parent abstract class Animal, create child classes Dog, Cat and Pig that implement the methods defined in the abstract class. In addition, create a constructor that takes a string parameter that represents the sound the animal makes and a toString method to provide a string representation of the object in each of the child classes. Your program should run with this diver without modification.

Animal Class:

public abstract class Animal {

public String sound;

public abstract void setSound(String sound);

public abstract String getSound();

}

AnimalDriver:

public class AnimalDriver {

public static void main(String[] args) {

Dog dog = new Dog("woof");

Cat cat = new Cat("meow");

Pig pig = new Pig("oink");

System.out.println(dog);

System.out.println(cat);

System.out.println(pig);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!