Pet Program
Description
This program uses the OOP Inheritance concept to design classes to represent Pet, Cat, and Dog objects.Each object, either cat or dog, has the name, breed, weight, and age as data members.
A dog object has an additional data member called "group".
A cat object has an additional data member called "body type".
Both dog and cat objects have a talk() method.
Cat: Meow, meow, meow
Dog: Woof, woof, woof
Dog: Woof, woof, woof
The superclass Pet defines the common features that apply to any types of pets.
Data members: name, breed, weight, and age
constructors
getters and setters
talk method
toString method
constructors
getters and setters
talk method
toString method
The subclasses Cat and Dog inherits the superclass Pet's features and adds additional features.
Cat: body type
Dog: group
Dog: group
In addition, both Cat and Dog classes override the superclass' talk() method. The Pet's talk method does not know what message should be returned by the subclasses.
This problem was originally presented as an example in An Introduction to Object-Oriented Programming with Java book. Authors: C. Thomas Wu
Additional features are added to meet our purpose.
Associated Concepts:
OOP - Inheritance
Algorithm:
Class diagrams:
Client program:
Client program:
The client program creates objects of Pet, Dog and Cat.
It then prints these objects and uses the object references to call talk() method.
It then prints these objects and uses the object references to call talk() method.
Source Code
Superclass Pet classSubclass Cat class
subclass Dog class
Client program