Java Reference
In-Depth Information
// Return the species
public String getSpecies() {
return species;
}
@Override
public void sound() {
System.out.println("Psst");
}
// Present a flea's details as a String
@Override
public String toString() {
return super.toString() + "\nIt's " + name + " the " + species;
}
protected String name;
// Name of flea!
protected String species;
// Flea species
}
Directory "DuplicateObjects"
The class has a copy constructor that creates a duplicate of the Flea object that is passed to it. It calls
the base class copy constructor to set the type . You have defined accessor methods for the name and the
species of flea. This is a common technique for providing access to private or protected data members of
a class when this is necessary. You also have a public method setName() that enables you to change the
name from outside of the class.
The base class needs a copy constructor too, so add the following definition to the Animal class:
public Animal(Animal animal) {
type = animal.type;
}
Directory "DuplicateObjects"
This sets the type member to the type for the object that is passed as the argument.
The Dog class needs a copy constructor too, and it is useful to provide methods to access the data mem-
bers:
public class Dog extends Animal {
public Dog(String aName) {
Search WWH ::




Custom Search