CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 21 Late Binding; OOP as a Racket Pattern Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Winter 2014.
CSE341: Programming Languages Introduction To Ruby; Dynamic OOP; "Duck Typing" Alan Borning Autumn 2012 (slides borrowed from Dan Grossman)
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming LanguagesSection 71 Programming Languages Section 7. Ruby: Fundamentals Xiaojuan Cai Spring 2015.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Winter 2013.
CSE 341 Programming Languages Introduction to Ruby Zach Tatlock Spring 2014.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
+ Ruby and other programming Languages Ronald L. Ramos.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Introduction to Ruby (adapted from CSE 341, Dan Grossman) 1.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
CSE 341 Programming Languages More Ruby Zach Tatlock Spring 2014.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
Alan Borning Spring 2018 (slides borrowed from Dan Grossman)
CSE341: Programming Languages Ruby: Blocks & Procs; Inheritance & Overriding Alan Borning Spring 2018.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Spring 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
CSE 341 Section 9 Fall 2017 Adapted from slides by Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011

This lecture Two separate topics Ruby's approach to almost-closures (blocks) and closures (Procs) –Convenient to use; unusual approach –Used throughout large standard library Explicit loops rare Instead of a loop, go find a useful iterator Subclasses, inheritance, and overriding –The essence of OOP –Not unlike in Java, but worth studying from PL perspective and in a more dynamic language Fall 20112CSE341: Programming Languages

Blocks Blocks are probably Ruby's strangest feature compared to other PLs –Normal: easy way to pass anonymous functions for all the reasons we have been studying –Normal: Blocks can take 0 or more arguments –Strange: Can send 0 or 1 block with any message send –Strange: Callee does not have a name for the block Calls it with yield, yield 42, yield (3,5), etc. Can ask block_given? but rarely used in practice (usually assume a block is given if expected, or that a block's presence is implied by other arguments) Fall 20113CSE341: Programming Languages

Examples Rampant use of blocks in standard library –Classes define iterators; don't write your own loops –Most of these examples happen to have 0 "regular" arguments Easy to write your own methods that use blocks Fall 20114CSE341: Programming Languages 3.times { puts "hi" } [4,6,8].each { puts "hi" } [4,6,8].each { |x| puts x * 2 } [4,6,8].map { |x| x * 2 } [4,6,8].any? { |x| x > 7 } # block optional [4,6,8].inject(foo) {|acc,elt| … } def silly a (yield a) + (yield 42) end x.silly 5 { |b| b*2 }

Blocks are "second-class" All a method can do with a block is yield to it (i.e., call it) –Can't return it, store it in an object (e.g., for a callback), etc. –But can also turn blocks into real closures (next slide) But one block can call another block via yield –From example MyList class in lec20.rb (though better in Ruby to use arrays as lists than define your own) Fall 20115CSE341: Programming Languages def map nil) {|x| yield x}) end

First-class closures Implicit block arguments and yield is often sufficient But when you want a closure you can return, store, etc.: –The built-in Proc class –lambda method of Object takes a block and makes a Proc Also can do it with "& arg", not shown here –Instances of Proc have a method call Fall 20116CSE341: Programming Languages def map_p proc nil) proc) end xs.map_p (lambda{|x| … })

Subclassing A class definition has a superclass ( Object if not specified) The superclass affects the class definition: –Class inherits all method definitions from superclass –But class can override method definitions as desired Unlike Java: –No such thing as "inheriting fields" since all objects create instance variables by assigning to them –Subclassing has nothing to do with a (non-existent) type system: can still pass any object to any method Fall 20117CSE341: Programming Languages class ColorPoint < Point …

Example (to be continued) Fall 20118CSE341: Programming Languages class Point attr_reader :x, :y attr_writer :x, :y def = = y end def distFromOrigin # direct field access + end def distFromOrigin2 # use getters Math.sqrt(x*x + y*y) end class ColorPoint < Point attr_reader :color attr_writer :color def initialize(x,y,c) = c end

An object has a class Using these methods is usually non-OOP style –Disallows other things that "act like a duck" –Nonetheless semantics is that an instance of ColorPoint "is a" Point but is not an "instance of" Point –Java's instanceof is like Ruby's is_a? Fall 20119CSE341: Programming Languages p = Point.new(0,0) cp = ColorPoint.new(0,0,"red") p.class # Point p.class.superclass # Object cp.class # ColorPoint cp.class.superclass # Point cp.class.superclass.superclass # Object cp.is_a? Point # true cp.instance_of? Point # false cp.is_a? ColorPoint # true cp.instance_of? ColorPoint # true

Why subclass Instead of creating ColorPoint, could add methods to Point –That could mess up other users and subclassers of Point Fall CSE341: Programming Languages class Point attr_reader :color attr_writer :color def = = = c end

Why subclass Instead of subclassing Point, could copy/paste the methods –Means the same thing if you don't use methods like is_a? and superclass, but of course code reuse is nice Fall CSE341: Programming Languages class ColorPoint attr_reader :x, :y, :color attr_writer :x, :y, :color def initialize(x,y,c="clear") … end def distFromOrigin + end def distFromOrigin2 Math.sqrt(x*x + y*y) end

Why subclass Instead of subclassing Point, could use a Point instance variable –Define methods to send same message to the Point –Often OOP programmers overuse subclassing –But for ColorPoint, subclassing makes sense: less work and can use a ColorPoint wherever code expects a Point Fall CSE341: Programming Languages class ColorPoint attr_reader :color attr_writer :color def = = c end def end … end

Overriding ThreeDPoint is more interesting than ColorPoint because it overrides distFromOrigin and distFromOrigin2 –Gets code reuse, but highly disputable if it is appropriate to say a ThreeDPoint "is a" Point –Still just avoiding copy/paste Fall CSE341: Programming Languages class ThreeDPoint < Point … def initialize(x,y,z) = z end def distFromOrigin # distFromOrigin2 similar d = super Math.sqrt(d*d + end … end

So far… With examples so far, objects are not so different from closures –Multiple methods rather than just "call me" –Explicit instance variables rather than whatever is environment where function is defined –Inheritance avoids helper functions or code copying –"Simple" overriding just replaces methods But there is a big difference (that you learned in Java): Overriding can make a method define in the superclass call a method in the subclass –The essential difference of OOP, studied carefully next lecture Fall CSE341: Programming Languages

Example: Equivalent except constructor Fall CSE341: Programming Languages class PolarPoint < Point def = = theta end def * end def * end def end … end Also need to define x= and y= (see lec20.rb) Key punchline: distFromOrigin2, defined in Point, "already works" –Why: calls to self are resolved in terms of the object's class def distFromOrigin2 Math.sqrt(x*x+y*y) end