CPE 102 Lab 5 - Inheritance

Goals:
Orientation:

You will write three classes that can make use of Inheritance, an Animal class, a Person class, and a Student class. There is a natural hierarchical relationship between these classes, especially when we think of sets.  The Student class is the most specific class type as a Student is a Person and a Person is an Animal but not all Animals are Persons and not all Persons are Students.  This lab explores how to represent such relationships in an Object Oriented way and how to take best advantage of the features of Inheritance when doing so.

What To Do:
  1. Write a class called Animal that has a single private instance variable of type int that represents the number of legs the animal has.  You should write a single constructor that takes an int parameter used to initialize the instance variable. Override the equals method (inherited from Object) so that it returns false if the reference passed in is null, or is not the same type as this, or doesn't have the same number of legs as this, otherwise true. Override the toString method inherited from Object so that it returns the message "I am an Animal object with N legs" where N is replaced with the actual number of legs the Animal object was constructed with.  Make sure your instance variable(s) are private and do not add any other methods. Test your Animal class before moving on.
  2. Write a class called Person that extends Animal. Person should have a single private instance variable of type String that represents the name of the Person. Write a single consructor that takes a String parameter used to initialize the instance variable AND an int parameter representing the number of legs the person has. Use the super() syntax to call the contructor of the super class Animal. Override the equals method (inherited from Animal that inherited it from Object) so that it returns false if the reference passed in is null, or is not the same type as this, or doesn't have the same number of legs as this, or does not have the same name as this, otherwise true. Override the toString method inherited from Animal that inherited it from Object so that it returns "I am an Animal object with N legs and a Person object whose name is XXX", where N is replaced with the correct number of legs for the Animal and XXX with the correct name for the Person. Make sure all instance variable(s) private and do not add any other methods. Test your Person class before moving on.

  3. Write a class called Student that extends Person. Student should have a single private instance variable of type double that represents the GPA of the Student. Write a single constructor that takes a double parameter used to initialize the instance variable AND a String representing the student's name AND an int representing the number of legs the student has. Use the super() syntax to call the constructor of the super class Person. Override the equals method (inherited from Person that inherited it from Animal that inherited it from Object) so that it returns false if the reference passed in is null, or is not the same type as this, or doesn't have the same number of legs as this, or does not have the same name as this, or does not have the same gpa as this, otherwise true.  Override the toString method inherited from Person that inherited it from Animal that inherited in from Object so that it returns "I am an Animal object with N legs and a Person object whose name is XXX and a Student Object with a Z.Z gpa", where N is replaced with the correct number of legs for the Animal and XXX with the correct name for the Person and Z.Z with the correct GPA for the Student (no special formatting of the GPA is required for this lab). Make sure all instance variable(s) private and do not add any other methods. Test your Student class before moving on.
  4. Test and experiment with these classes until you feel you have a good sense of how inheritance works and what benefits it can provide.  Discuss your views with your other students to see if your insights agree with theirs. Ask for assistance from your instructor to clarify any ambiguities or disagreements you encounter.

Lab courtesy of Julie Workman.