< Back

Lecture 6- Inheritance


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab6 
$ cd ~/cs170/labs/lab6

An Introduction to Inheritance

We now understand how classes can represent our own data types. This is incredibly neat, and even allows for the efficient reuse of code. However, there are some instance where your current knowledge of classes would cause your code to not be very reusable. For example, what if something doesn't fit exactly into the templates we've created for our objects? One mechanism to fix this problem is inheritance, which allows us to specify the relationships between classes.



Lab Activity 1
Dogs

One of the more classical examples of Inheritance is describing animals. There is a well known hierarchy of animals, based off of genus, species, phylum, etc. However, that can be a little bit complicated and overwhelming. So, we'll focus on one common type of animal: dogs.

This is a very simplistic, directed, and less that useful example of inheritance. Hopefully it give you a better understanding of the syntax and the meaning behind inheritance.

Details

  1. The file dog.py contains a declaration for a Dog class. Save this file to your directory and study it. Notice what instance variables and methods are provided. The file also contains some code that runs the actual program. Make sure you understand what it does, and how.

  2. One use of inheritance is to change the behavior of specific methods. Create a a class called Pomeranian that extends Dog. Override the speak method of the Dog class by defining a method in the Pomeranian class with the same method signature. The method should print something typical of a Pomeranian.

    Modify the program to add statements to create an instance of the Pomeranian class and make it speak (do not remove Spot). Run the program. Both dogs should speak something different because both classes have different bodies for the speak method. Notice that you did not define the __init__ method for the Pomeranian class and yet you are still able to call it because it was inherited from the Dog class.

  3. In addition to replacing the behavior of a method in the parent class it is also possible to extend the behavior of a method in the parent class. Create a class called Labrador in the same file that also extends Dog. Add a second parameter to the __init__ method that specifies the color of the lab and stores the lab's color in an attribute. Again override the speak method with something typical of a lab. Also include something about the lab's color in the speak print statement.

    Modify your program by adding statements to create a Labrador. Be sure to call the constructor method that specifies the name and the color. Also add a statement to make it speak. Run the program. The dog and Pomeranian should speak, but there should be an error with the lab. The name attribute is not defined because the Dog class's __init__ method was overridden in the Labrador class. To fix this add a call to the Dog class's __init__ method by using built-in method super by adding the following statement to the Labrador's __init__ method:

      super().__init__(name)
    

    Run the dog_test program to verify that the program now works.

Example

>>> spot = Dog("Spot")
>>> spot.speak()
Spot says Woof.
>>> bandit = Pomeranian("Bandit")
>>> bandit.speak()
Bandit says Squeak.
>>> spike = Labrador("Spike", "Blond")
Spike, the Blond Labrador says Bark.
Lab Activity 2
College Directory

As you are probably aware, the college keeps a directory of everyone affiliated with the college accessible for anyone to search. It is located on the main page of roanoke.edu. Everyone has certain information, but there is more information stored for certain classifications of people, such as Faculty or Staff. One mechanism to make storage of this information is to create separate classes for each of the classifications of people, but inheritance will eliminate a lot of duplicated code.

Details

Create 3 different files for our 3 different classifications of people:  People , Student, and Faculty. We aren't going to worry too much about actions that these classes can take, but you will want a __str__ method for each one of them. Both Student and Faculty will inherit the People class. You simply need to work out what information is common between Students and Faculty; Any shared information should go into their shared parent class, People.

For the sake of this program, you should store the following information (but only for the appropriate people):

Once you have finished with the class definitions, test them by creating several Students and Faculty. Make sure you can access the appropriate attributes of each object, and that you are not storing any unnecessary attributes for each class.

Example

scotty = Faculty("Scotty Smith", "1/1/1900", "chssmith@roanoke.edu", "Trexler 365-B", "MCSP Department\n221 College Lane\nSalem, Va 24153")
scotty_student = Student("Scotty Smith", "1/1/1900", "cssmith@mail.roanoke.edu", "Crawford 218", "Box 1952")
print(scotty)
#Prints:
#Scotty Smith
#DOB - 1/1/1900
#Email - chssmith@roanoke.edu
#Office - Trexler 365-B
#Mail - MCSP Department
#221 College Lane
#Salem, Va 24153

print(scotty_student)
#Prints:
#Scotty Smith
#DOB - 1/1/1900
#Email - cssmith@mail.roanoke.edu
#Dorm - Crawford 218
#Mail - Box 1952

 

Challenge

For gradebook, we used a Dictionary to store the list of students. However, we aren't always going to be able to use Dictionaries to solve our problems. In fact, using a Dictionary for something like the directory search would be a bit of a waste of memory.

Instead, we could simply store all of the people you have created into a list. Then, whenever someone is looking for information for a particular person we just have to find the entry in that list.

Modify your program so you can interactively search and print information about people. You should use a list this time, instead of a dictionary.


Submission

When you have finished, create a tar file of your lab6 directory. To create a tar file, execute the following commands:

cd ~/cs170/labs
tar czvf lab6.tgz lab6/

To submit your activity, go to inquire.roanoke.edu. You should see an available assignment called Lab Assignment 6. Make sure you include a header listing the authors of the file.


In-Class Notes