Inheritance
You will create a class called Pet
and an inherited class called Dog.
Pet
|
# name: String
# age : int
|
+ toString():String |
Dog
|
- breed: String
- treat : String |
+ toString():String |
In the Pet.java file:
- You will write a constructor,
which will initialize Pet's two pieces of protected instance data: name and
age. These will get passed as parameters to the constructor.
- You will write a toString method,
which will return: "name is age years old." (Where
name and age are the values of the name and age variables. ex:
Lady is 5 years old.)
In the Dog.java file:
- Write a constructor which will
have 4 paramters: name, age, breed, and favorite treat. Call the parent's
constructor and pass name and age to it. Initialize Dog's instance data, breed
and treat.
- Write toString -- (Note: Dog's
toString() overrides Pet's toString() ) first call the parent's toString,
and append to that string: "name is a breed. name's
favorite treat is treat."
(Where name, breed, and treat are the values of the name, breed
and treat variables.)
Write a driver program which will
instantiate one Pet object and two Dog objects. Make up data to pass to the
constructors. Then print out each object you instantiated.
Once you have your program working,
show your lab to the Peer Instructor to be checked off.