Download presentation
Presentation is loading. Please wait.
Published byFelix Stevenson Modified over 6 years ago
1
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College
Java: An Eventful Approach An innovative approach to teaching Java in CS1† Inheritance Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE
2
Topic: Inheritance We cover inheritance very late
Even though we use it with WindowController and ActiveObject from day 1. “Subtype polymorphism” already obtained with interfaces. Inheritance is important for O-O programming, but how important for CS 1. Demo living broccoli
3
Inheritance is Hard! Inheritance is code reuse
When do you normally teach code reuse in your curriculum? Inheritance is very complicated Loss of locality of code Ping-Pong code Only cover very simple uses of inheritance Only 1 to 2 lectures on inheritance Demo living broccoli
4
Simple Subclass public class LaundryImpl implements Laundry {
private DrawableInterface[] parts; // the shapes of components private int maxParts; // max number of parts of laundry private int numParts; // current number of parts of laundry private double upperX, upperY // coords of laundry item // basis for all laundry constructions protected LaundryImpl (double x, double y, int maxLaundryParts) { upperX = x; upperY = y; maxParts = maxLaundryParts; numParts = 0; parts = new DrawableInterface[maxParts]; }
5
More LaundryImpl // add a component to laundry item
public void addComponent(DrawableInterface newPart) { if (numParts < maxParts) { parts[numParts] = newPart; numParts++; } // Move laundry relative to its current location public void move( double x, double y ) { for (int i = 0; i < numParts; i++) { parts[i].move(x, y); upperX = upperX + x; upperY = upperY + y;
6
Extending LaundryImpl
public class Tshirt extends LaundryImpl { private static final int numTShirtParts = 6; // Create a new shirt at the specified location. public Tshirt( double x, double y, DrawingCanvas canvas ) { super (x, y, numTShirtParts); // Construct background trim addComponent(new FramedRect( … )); // construct interior (except neck) addComponent(new FilledRect( … )); // add neck and neck trim addComponent(new FilledOval( … )); addComponent(new FramedOval( … )); }
7
Adding and Overriding Methods
Adding new methods is straightforward. Overriding bit trickier. Use falling object example. Demo living broccoli
8
Overriding methods public class FallingObject extends ActiveObject {
private int screenHeight; // lowest point in window private double yspeed; // speed of fall protected Resizable2DInterface object; // the object // Never initialized! // construct object and put at top of screen protected FallingObject(double speed, int aScreenHeight) { yspeed = speed; screenHeight = aScreenHeight; }
9
// Object falls to ground
public void run() { while (object.getY() < screenHeight ) { pause(DELAY_TIME); object.move(0,yspeed); } hitBottom(); // Action to be performed when falling object hits ground // Here remove from canvas protected void hitBottom() { object.removeFromCanvas();
10
Some extensions easy public class FallingLeaf extends FallingObject {
// construct leaf and put at top of screen at x from left public FallingLeaf(DrawingCanvas canvas, Image leafpic, double x, double speed, int aScreenHeight) { super(speed, aScreenHeight); object = new VisibleImage(leafpic, -100, 0 ,canvas); object.move(x , - object.getHeight()); start(); }
11
Others need more work public class Tomato extends FallingObject {
private double height; private double xOffset; public Tomato (DrawingCanvas aCanvas, double x, int groundHeight) { // Create a generic falling object super (BALL_SPEED, groundHeight); … // Specialize it to be a ball object = new FilledOval (…); object.setColor (Color.red); // Start it falling start(); }
12
Others need more work // squish tomato protected void hitBottom() {
object.moveTo(xOffset-BALL_SIZE/2, height-BALL_SIZE/3); object.setHeight(BALL_SIZE/3); object.setWidth(2*BALL_SIZE); }
13
Bottom Line w/ inheritance
Inheritance not as important as dynamic method invocation. Mainly useful for code reuse. Also explain some of the use of inheritance in the library. Don’t expect them to master inheritance until CS 2.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.