Question
This lab provides the superclass called Animal and the driver called TestInheritance (which contains main where the program starts). Your job is to add a
This lab provides the superclass called Animal and the driver called TestInheritance (which contains main where the program starts). Your job is to add a new subclass called Dog which uses the superclass Animals Name through inheritance (by using the keyword extends) and adds the breed in the Dog subclass. You add this new subclass Dog to the template provided after the following line: "//add subclass Dog using Inheritance after this line" Do not make any other changes to the code in the template. So all animals have a name as defined in the superclass Animal but dogs also have a breed as defined in the subclass Dog. You should review Zybooks Chapter 10 available subsections on Inheritance prior to attempting this lab. There is no input data from the user to run this program.
//this is the superclass class Animal { private String name; public void setName (String n) { name = n; } public String getName() { return name; } }
//add subclass Dog using inheritance after this line
//this is the driver where the program starts public class TestInheritance { public static void main(String[] args) { Dog dg = new Dog(); Dog dg1 = new Dog(); dg.setName("Lassie"); dg.setBreed("collie"); dg1.setName("Benji"); dg1.setBreed("mixed"); System.out.println("Dog Name: " + dg.getName()); System.out.println("Dog Breed: " + dg.getBreed()); System.out.println("Dog Name: " + dg1.getName()); System.out.println("Dog Breed: " + dg1.getBreed()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started