0% found this document useful (0 votes)
8 views4 pages

Modify DogTest for Yorkshire and Labrador

The document provides instructions for modifying Java classes related to inheritance, specifically focusing on a Dog class and its subclasses, Labrador and Yorkshire. It outlines steps to fix constructor issues, implement an abstract method for average breed weight, and handle compilation errors. Additionally, it emphasizes understanding the relationships between classes and the importance of proper method implementation in subclasses.

Uploaded by

vangelgable5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Modify DogTest for Yorkshire and Labrador

The document provides instructions for modifying Java classes related to inheritance, specifically focusing on a Dog class and its subclasses, Labrador and Yorkshire. It outlines steps to fix constructor issues, implement an abstract method for average breed weight, and handle compilation errors. Additionally, it emphasizes understanding the relationships between classes and the importance of proper method implementation in subclasses.

Uploaded by

vangelgable5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exploring Inheritance

File [Link] contains a declaration for a Dog class. Save this file to your directory and study it—notice what
instance variables and methods are provided. Files [Link] and [Link] contain declarations for
classes that extend Dog. Save and study these files as well.

File [Link] contains a simple driver program that creates a dog and makes it speak. Study [Link],
save it to your directory, and compile and run it to see what it does. Now modify these files as follows:

1. Add statements in [Link] after you create and print the dog to create and print a Yorkshire and a
Labrador. Note that the Labrador constructor takes two parameters: the name and color of the labrador,
both strings. Don’t change any files besides [Link]. Now recompile [Link]; you should get an
error saying something like

./[Link]: Dog([Link]) in Dog cannot be applied to ()


{
^
1 error

If you look at line 18 of [Link] it’s just a {, and the constructor the compiler can’t find (Dog()) isn’t
called anywhere in this file.
a. What’s going on? (Hint: What call must be made in the constructor of a subclass?)
=>

b. Fix the problem (which really is in Labrador) so that [Link] creates and makes the Dog,
Labrador, and Yorkshire all speak.

2. Add code to [Link] to print the average breed weight for both your Labrador and your Yorkshire.
Use the avgBreedWeight() method for both. What error do you get? Why?

=>

Fix the problem by adding the needed code to the Yorkshire class.

3. Add an abstract int avgBreedWeight() method to the Dog class. Remember that this means that the word
abstract appears in the method header after public, and that the method does not have a body (just a
semicolon after the parameter list). It makes sense for this to be abstract, since Dog has no idea what breed
it is. Now any subclass of Dog must have an avgBreedWeight method; since both Yorkshire and Laborador
do, you should be all set.

Save these changes and recompile [Link]. You should get an error in [Link] (unless you made more
changes than described above). Figure out what’s wrong and fix this error, then recompile [Link]. You
should get another error, this time in [Link]. Read the error message carefully; it tells you exactly what
the problem is. Fix this by changing DogTest (which will mean taking some things out).

154 Chapter 9: Inheritance


// ****************************************************************
// [Link]
//
// A class that holds a dog's name and can make it speak.
//
// ****************************************************************
public class Dog
{
protected String name;

// ------------------------------------------------------------
// Constructor -- store name
// ------------------------------------------------------------
public Dog(String name)
{
[Link] = name;
}

// ------------------------------------------------------------
// Returns the dog's name
// ------------------------------------------------------------
public String getName()
{
return name;
}

// ------------------------------------------------------------
// Returns a string with the dog's comments
// ------------------------------------------------------------
public String speak()
{
return "Woof";
}
}

Chapter 9: Inheritance 155


// ****************************************************************
// [Link]
//
// A class derived from Dog that holds information about
// a labrador retriever. Overrides Dog speak method and includes
// information about avg weight for this breed.
//
// ****************************************************************
public class Labrador extends Dog
{
private String color; //black, yellow, or chocolate?
private int breedWeight = 75;

public Labrador(String name, String color)


{
[Link] = color;
}

// ------------------------------------------------------------
// Big bark -- overrides speak method in Dog
// ------------------------------------------------------------
public String speak()
{
return "WOOF";
}

// ------------------------------------------------------------
// Returns weight
// ------------------------------------------------------------
public static int avgBreedWeight()
{
return breedWeight;
}
}

156 Chapter 9: Inheritance


// *****************************************************************
// [Link]
//
// A class derived from Dog that holds information about
// a Yorkshire terrier. Overrides Dog speak method.
//
// *****************************************************************

public class Yorkshire extends Dog


{
public Yorkshire(String name)
{
super(name);
}

// -------------------------------------------------------------
// Small bark -- overrides speak method in Dog
// -------------------------------------------------------------
public String speak()
{
return "woof";
}
}

// ****************************************************************
// [Link]
//
// A simple test class that creates a Dog and makes it speak.
//
// ****************************************************************

public class DogTest


{
public static void main(String[] args)
{
Dog dog = new Dog("Spike");
[Link]([Link]() + " says " + [Link]());
}
}

Chapter 9: Inheritance 157

You might also like