cs 102: inheritance audrey st. john. overview inheritance as a concept for code reuse in...

15
CS 102: Inheritance Audrey St. John

Upload: cori-blankenship

Post on 17-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

CS 102: Inheritance

Audrey St. John

Overview

Inheritance As a conceptFor code reuseIn ActionScript 3.0

Superclass, subclass terminologyInheritance hierarchyAS3 specifics

Keywords extends, super

Inheritance hierarchy

“is a”

Subclass

Superclass

Subclass

Superclass

What does this have to do with code?

public class Swan { private var heart : Heart; … public sleep() : void {…} public breathe() : void {…} …}

public class Penguin { private var heart : Heart; … public sleep() : void {…} public breathe() : void {…} …}

public class Crow { private var heart : Heart; … public sleep() : void {…} public breathe() : void {…} …}

This is awfully

repetitive!

Inheritance gives us

code reuse!

Class exercise

Create hierarchy for:Wind CelloInstrumentFluteStringClarinetViolin

Mmm… brownies!

Inheritance in ActionScript 3.0

Using the keyword extendsYou’ve already been using this!public <classname> extends MovieClip

A subclass inherits all the properties and methods of its superclassPseudo-inheritance: private properties

If no explicit superclass is provided via extends, an ActionScript class implicitly extends Object

Brownie.as

package { import flash.display.MovieClip; import flash.text.TextField;

/** * Brownie class for printing a brownie recipe. * @author Audrey Lee **/ public class Brownie extends MovieClip {

private var recipe : String;

/*** Constructor creates a default Brownie recipe.**/public function Brownie() { // the lazy way... recipe = "Purchase Brownie mix. Bake according to package.";}

/*** Override the Object's toString() method.* Get the recipe for the Brownie.* @return a String that holds the recipe.**/public override function toString():String { return recipe;}

/** * Print the recipe to the text field.**/public function printRecipe():void { recipeField.text = toString();}

}}

/*** Override the Object's toString() method.* Get the recipe for the Brownie.* @return a String that holds the recipe.**/public override function toString():String { return recipe;}

/** * Print the recipe to the text field.**/public function printRecipe():void { recipeField.text = toString();}

DeluxeBrownie.as

package { /** * DeluxeBrownie is a Brownie that additionally has a mix-in. * @author Audrey Lee **/ public class DeluxeBrownie extends Brownie { private var mixin : String;

/** * Constructor requires a mixin parameter. **/ public function DeluxeBrownie( mixer : String ) { super();// call the superclass (Brownie) constructor mixin = mixer; }

/** * Override the toString method to mix in the new ingredient. **/ public override function toString():String { // use super's toString method to keep basic recipe return super.toString() + "\nBefore baking, mix in " + mixin + "."; } }

}

UltimateBrownie.as

package { /** * UltimateBrownie is a DeluxeBrownie that additionally has a topping. * @author Audrey Lee **/ public class UltimateBrownie extends DeluxeBrownie { private var topping : String;

/** * Constructor requires a mixin and a topping parameter. **/ public function UltimateBrownie( mixer : String, topper : String { super( mixer ); // call the superclass (DeluxeBrownie) constructor topping = topper; }

/** * Override the toString method to mix in the new ingredient. **/ public override function toString() : String { // use super's toString method to keep deluxe recipe return super.toString() + " After baking, top with " + topping +

"."; } }}

And now…

var brownie : Brownie = new Brownie(); brownie.x = 20; brownie.y = 5; addChild( brownie ); brownie.printRecipe(); var deluxeBrownie : DeluxeBrownie = new DeluxeBrownie( "chocolate chips" ); deluxeBrownie.x = 20; deluxeBrownie.y = 100; addChild( deluxeBrownie ); deluxeBrownie.printRecipe();

var ultimateBrownie : Brownie = new UltimateBrownie( "pecans", "caramel sauce" ); ultimateBrownie.x = 20; ultimateBrownie.y = 195; addChild( ultimateBrownie ); ultimateBrownie.printRecipe();

How did the ultimate recipe get printed?

ultimateBrownie.printRecipe();

No implementation in UltimateBrownie, so check superclass DeluxeBrownie

No implementation in DeluxeBrownie, so check superclass BrownieFound it!

printRecipe calls toString

In Brownie class:

How does toString() method evaluate? Implemented in UltimateBrownie

Calls superclass DeluxeBrownie toString()

Calls superclass Brownie toString()

public override function toString():String { // use super's toString method to keep deluxe recipe return super.toString() + " After baking, top with " + topping + "."; }

public function printRecipe():void {

recipeField.text = toString();

}

ppublic override function toString():String { // use super's toString method to keep basic recipe return super.toString() + "\nBefore baking, mix in " + mixin + ".";}

public override function toString():String { return recipe; }

Purchase Brownie mix. Bake according to package.Purchase Brownie mix. Bake according to package.Before baking, mix in pecans. Purchase Brownie mix. Bake according to package.Before baking, mix in pecans. After baking, top with caramel sauce.

private vs. protected vs. public

private properties and methods can only be accessed by the class (and not by subclasses!)

protected properties and methods can be accessed subclasses

public properties and methods can be accessed by everyone

ActionScript warning!

Repeating properties in a subclass can cause undesired behavior!

package {public class Mystery { private var x : int;

public function Mystery() { x = 1; }

public function evaluate():int { return x * x; }}}

package {public class BadMystery1 extends Mystery { private var x : int;

public function BadMystery1() { x = 2; }}}

package {public class BadMystery2 extends Mystery { private var x : int;

public function BadMystery2() { x = 3; }

public override function evaluate():int { return x * x * x; }}}1 1 27