LMICSE: Lego Mindstorms in Computer Science Education

Site Map | Contact Us
Project Overview | Staff | Grant Information
Short Workshops | Primary Workshops
CS 1 | Data Str. & Algo. | Prog. Languages | Architecture | Intelligent Sys. | Operating Sys. | Net-centric
Ada | C | C++ | Java | Lisp

Lab 11: Inheritance

small logo

Inheritance

A class can be related to another class by inheritance. This means that it has all the instance variables and methods that its "parent" class does, but it may extend its parent by adding additional methods or modifying their behavior. These additional methods and behaviors are accessible by a "child" object, but not by a "parent" object.

Here we can see that the syntax for defining a class that inherits or extends another is straightforward:

class Parent {
	// definitions
} class Child extends Parent { // definitions }
So here, if we were to code our Parent class with just a method that would send our Turtle driving forward, and our Child class with just a method that would stop our Turtle, then we would be able to tell our Child to both go forward, and stop. Our parent however, would not have the capability to stop.

Prerequisite Knowledge

This lab assumes that you have the knowledge to properly write methods and use control statements, as well as the knowledge to control the Turtle's movements.

Materials

Basic RCX Turtle with bump sensors.

Task One

Using task1.java (you'll recognize this from the lab on building classes), rewrite the Note class so that it is a child of a new class called MusicThing. Next, create another class called Rest, that also extends MusicThing. This can be done by simple copying, pasting, and editing the Note class. From there you need to:

  • Remove any irrelevant methods from Note and Rest (for example, in the Note class, we have 2 constructors, one for playing a note, and one for playing a rest)
  • Revise the play method for each class to just handle the kind of thing being played
  • Revise the main program to make instances of Note and Rest where needed
  • Make any other adjustments that are necessary

Task Two

Now, revise your code from Task 1 so that the code in common between the Note class and the Rest class is moved into the MusicThing class. Since both of these classes extend the MusicThing class, they will both inherit all of the methods and variables declared in MusicThing, thus there is no need to have the same methods declared twice when they can be declared once. By moving all of the common code into the MusicThing class, our 'child' class now actually inherits something.

Task Three

In the past two tasks, we modified a file to allow for inheritance. With this task, we are going to work from the ground up to build a program that uses inheritance. You need to write a program in which:

  • There is a parent class called 'Figure,' and three child classes
  • The turtle should be able to "draw" three kinds of figures: equilateral triangles, isosceles right triangles, and squares
    • each figure will be a child class, and in each class will be a draw() method that has the Turtle map out that figure
  • Each figure, when it is instantiated, should have an initial size that it "remembers," so if it's drawn several times, it will be the same size each time
  • There should also be a "scale factor" that allows the caller to make all the figures uniformly larger or smaller by some multiplicative factor (this means that the scale factor will need to be accessible by all of the figures ie. inherited)
Note: When drawing your isosceles right triangles, you will need to use some math functions to figure the other turning angles, and the length of the hypotenuse side. Also, when drawing your figures, don't forget to multiply the side length by the multiplicative factor.

Some Additional Problems

  • Add classes for other types of figures, such as pentagons and hexagons
  • Modify your code so that the scale factor increases the side length by a power (squared, cubed, etc.), rather than a multiplier

Conclusion

In this lab, you learned the following:

  • What kinds of items are passed down through inheritance
  • When inheritance is useful
  • How inheritance can be used to share information among classes