COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

OOP - Inheritance Pet Program
Pet.java
Cat.java
Dog.java
PetClient.java
Payroll Program
Employee.java
SalariedEmployee.java
HourlyEmployee.java
CommissionEmployee.java
BasePlusCommissionEmployee.java
PayrollClient.java
Class Roster Program
Student.java
UndergraduateStudent.java
GraduateStudent.java
ClassRosterClient.java




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



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

The subclasses Cat and Dog inherits the superclass Pet's features and adds additional features.

Cat: body type

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:

The client program creates objects of Pet, Dog and Cat.

It then prints these objects and uses the object references to call talk() method.

Source Code

Superclass Pet class



Subclass Cat class



subclass Dog class



Client program



Sample Run



Video