'&$%CSE 341:Programming LanguagesDan GrossmanWinter 2008Lecture 20— Inheritance & Overriding, Blocks & IteratrorsDan Grossman CSE341 Winter 2008, Lecture 20 1'&$%TodayTwo separate topics (mostly the first one)1. Subclassing (inheritance, overriding, dynamic-dispatch, somedesign issues)2. Blocks and iterators (closures in Ruby)Section: Essential stuff for homework 6• Arrays and iterators on them• Hashes• More on blocks and iteratorsDan Grossman CSE341 Winter 2008, Lecture 20 2'&$%SubclassesRuby is dynamically typed, so subclassing is not about whattype-checks.Subclassing is about inheriting methods from the superclass.• In Java, it’s about inheriting fields too, but we can just write toany field we want.Example: ThreeDPoint inherits methods x and y.Example: ColorPoint inherits distFromOrigin anddistFromOrigin2.Dan Grossman CSE341 Winter 2008, Lecture 20 3'&$%OverridingIf it were just inheritance, then with dynamic typing subclassing wouldjust be avoiding copy/paste.It’s more.But first, “simple” overriding lets us redefine methods in the subclass.• Often convenient to use super to use superclass definition in ourdefinition.This is still “just” avoiding copy-paste.Example: distFromOrigin and initialize in ThreeDPoint.Dan Grossman CSE341 Winter 2008, Lecture 20 4'&$%Ruby-ish DigressionWhy make a subclass when we could just add/change methods to theclass itself?• Add a color field to Point itself• Affects all Point instances, even those already created (!)Plus: Now a ThreeDPoint has a color field too.Minus: Maybe that messes up another part of your program.Fun example: Redefining Fixnum’s + to return 5.Dan Grossman CSE341 Winter 2008, Lecture 20 5'&$%Late-BindingSo far, this OO stuff is honestly very much like functional programming• Fields are just like things in a closure’s environmentBut this is totally different:• When a method defined in a superclass makes a self call itresolves to the method defined in the subclass (typically viaoverriding)Example: distFromOrigin2 in PolarPoint still works correctly!!!Friday’s lecture: Studying this very carefully.Dan Grossman CSE341 Winter 2008, Lecture 20 6'&$%Blocks and IteratorsMany methods in Ruby “take a block,” which is a “special” thingseparate from the argume nt list.They are used very much like closures in functional programming; cantake 0 or more arguments (s ee examples)The preferred way for iterating over arrays, doing something n times,etc.They really are closures (can access local variables where they weredefined).Useful on homework: any?, eachUseful in Ruby: many, many moreDan Grossman CSE341 Winter 2008, Lecture 20 7'&$%Blocks vs. ProcsThese block arguments can be used only by the “immediate” callee viathe yield keyword.If you really want a “first-c lass object” you can pass around, store infields, etc., convert the block to an instance of Proc.• lambda {|x,y,z| e}• Instances of Proc have a method call• This really is exactly a closure.Actually, there is a way for the caller to pass a block and the calleeconvert it to a Proc.• Look it up if you’re curious.• This is what lambda does (just a method in Object that returnsthe Proc it creates)Dan Grossman CSE341 Winter 2008, Lecture 20
View Full Document