CSE 130: Spring 2012

Similar documents
Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Programming Languages

Programming Languages

Lecture 24: Implementing Heaps 10:00 AM, Mar 21, 2018

Overview. Elements of Programming Languages. Objects. Self-Reference

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Programming Languages

Data Structures (list, dictionary, tuples, sets, strings)

Overview. Elements of Programming Languages. Objects. Self-Reference

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Inheritance (Chapter 7)

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Pong in Unity a basic Intro

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

Dependent Object Types - A foundation for Scala s type system

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

CSE : Python Programming

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Exercise 8 Parametric polymorphism November 18, 2016

CSC207 Week 3. Larry Zhang

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018

1: Introduction to Object (1)

CS Programming Languages: Scala

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

Software Engineering: Design & Construction

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

Using the API: Introductory Graphics Java Programming 1 Lesson 8

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

17 From Delegation to Inheritance

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Day 5. COMP1006/1406 Summer M. Jason Hinek Carleton University

Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Object-Oriented Design Lecture 16 CS 3500 Fall 2010 (Pucella) Friday, Nov 12, 2010

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance and Polymorphism in Java

17 Multiple Inheritance and ADT Extensions

Object-oriented programming

CS-202 Introduction to Object Oriented Programming

Inheritance (Outsource: )

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Concepts of Programming Languages

Life in a Post-Functional World

Inheritance. Transitivity

Lecturer: William W.Y. Hsu. Programming Languages

Software Engineering Design & Construction

Programming Languages

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

MITOCW watch?v=kz7jjltq9r4

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Atelier Java - J2. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

CS162 Week 1. Kyle Dewey. Friday, January 10, 14

Section 05: Solutions

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Programming Exercise 14: Inheritance and Polymorphism

Representation Invariants and Abstraction Functions

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CSE341, Fall 2011, Lecture 26 Summary

Clarifying Roles. Jonathan Worthington German Perl Workshop 2007

Data Abstraction. Hwansoo Han

The Stack, Free Store, and Global Namespace

CS558 Programming Languages

20 Subclassing and Mutation

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

7 Code Reuse: Subtyping

Lecture 3 More OOP. CS 2113 Software Engineering Tim Wood. The George Washington University

Scala. Fernando Medeiros Tomás Paim

First Programming Language in CS Education The Arguments for Scala

Lecture 8: Summary of Haskell course + Type Level Programming

Homework 9 CSCI 334: Spring, 2019

JAVA MOCK TEST JAVA MOCK TEST II

Programming Paradigms

Node.js Training JavaScript. Richard richardrodger.com

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

PREPARING FOR PRELIM 2

21 Subtyping and Parameterized Types

Intro. Scheme Basics. scm> 5 5. scm>

Inheritance. COMP Week 12

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CS 61B Discussion 4: Inheritance Fall 2018

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Assignment 2. Application Development

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Transcription:

CSE 130: Spring 2012 Objects, Classes & Inheritance Ranjit Jhala, UC San Diego What is an Object? What is an object? Here s a really simple object object countup { var count = 0 def next() = { count += 1 count // Data // Function def hasnext = true // Function Object = Map from Names To Properties Names = count, next, hasnext Properties = Data + Functions What is an object Just a fancy value Object = Map from Names To Properties Properties = Data + Functions 1

So, how can we USE an Object? So, how can we USE an Object? Here s a client function: scala> def tickn(it: Any, n: Int) = for (i <- 1 to n){ println(it.next()) scala> tickn(countup, 5) What happens? So, how can we USE an Object? Here s a client function: def tickn(it: Any, n: Int) = for (i <- 1 to n){println(it.next()) Type checker is unhappy. <console>:9: error: value next is not a member of Any println(it.next()) ^ How to DESCRIBE the Required Properties? How to DESCRIBE the Required Properties? <console>:9: error: value next is not a member of Any println(it.next()) ^ Duh. Cannot call tickn with Any value... Which properties required by tickn 2

Describing Objects with Structural Types The mystery type??? def tickn(it:???, n: Int) = for (i <- 1 to n){println(it.next()) it must map next to a function { def next(): Any Called a Structural Type Describes requirements on structure... Need to DESCRIBE the TYPE of an object? def tickn(it: { def next(): Any, n: Int) = for (i <- 1 to n){println(it.next()) Thats it! Lets take it for a spin. scala> tickn(countup, 5) 1 2 3 4 5 Recap Object = Names -> Properties Type = Names -> Types 3

What is an object? Here s another really simple object object countfib { var a = 0 var b = 1 // Data // Data def next() = { // Function val (a_, b_) = (b, a + b) a = a_ b = b_ a def hasnext = true // Function Object = Map from Names To Properties Names = a, b, next, hasnext Properties = Data + Functions So, how can we USE an Object? We can use it with tickn too! scala> tickn(countfib, 10) 1 2 3 5 8 13 21 34 55 89 tickn doesn t care about extra names and properties 4

So, how can we USE an Object? tickn can be called with any object with suitable next A structural subtype of {def next():any scala> tickn("yellowmattercustard".iterator, 5) y e l l o So, how can we USE an Object? tickn can be called with any object with suitable next scala> tickn(list("cat", "dog").iterator, 5) cat dog java.util.nosuchelementexception: next on empty iterator Whoops. So, how can we USE an Object? We can add more required methods to tickn def tickn[a](it: {def next(): A; def hasnext: Boolean, n: Int) = for (i <- 1 to n){ if (it.hasnext) println(it.next()) So, how can we USE an Object? We can add more required methods to tickn Better to give the new required type a name type ticker = { def next(): Any; def hasnext: Boolean 5

and then... def tickn(it: ticker, n: Int) = for (i <- 1 to n){ if (it.hasnext) { println(it.next()) else { println("out of Stuff!") So, how can we USE an Object? Which works quite nicely... scala> tickn(list("cat", "dog").iterator, 5) cat dog Out of Stuff! Out of Stuff! Out of Stuff! Recap Objects Maps from names to properties Properties = Data + Functions (+ Objects) Types Short descriptions of Objects Maps from names to types Structural Types So thats OOP! (?) What else do we need? 6

What IS a class Anyway? What IS a class Anyway? Seem to be doing just fine without them! Many OO languages are Class-less Google s Go JavaScript (also type-less... ) Java (only interfaces) Why do we need Classes? You tell me!???????????? What is a Class? Unified mechanism for two tasks Specification A name for describing contents Implementation A template for creating new objects What is a Class? Spectrum from Specification to Implementation 7

1. Interfaces Only Specification of Types 2. Abstract Class Specification + Some Concrete Implementations 3. Class Specification + All Concrete Implementations The Class Spectrum in Scala: Interfaces The Class Spectrum in Scala: Interfaces Interfaces: Only Specify Type type ticker = { def next(): Any; def hasnext: Boolean Some other ways too (later.) The Class Spectrum in Scala: Abstract Classes The Class Spectrum in Scala: Abstract Classes Lets make a ScreenSaver! Here s the basic loop for the graphics: def run(box: JPanel, points: List[Point]) {... while (true) { for (p <- points) { p.draw(g) p.move(d, delta) p.collide(d)... 8

Need a type Point describing objects that we can: draw, move, collide The Class Spectrum in Scala: Abstract Classes Need a type describing draw, move, collide First, some essential fields... abstract class Point { // Must be filled in by (concrete) sub-classes val name : String // immutable var x : Int // mutable var y : Int // mutable // Defined, but may be "overridden" by sub-classes var xvel = Rand(1, 5) var yvel = Rand(1, 5)... The Class Spectrum in Scala: Abstract Classes Need a type describing draw, move, collide Next, the methods... abstract class Point { // Must be filled in by (concrete) sub-classes def xdim : Int def ydim : Int def render(g: Graphics): Unit def move(d: Dimension, delta: Int)...... these are pure specification must be filled in by any actual Point object 9

The Class Spectrum in Scala: Abstract Classes Need a type describing draw, move, collide Next, we specify the methods... abstract class Point { // Must be filled in by (concrete) sub-classes def xdim : Int def ydim : Int def render(g: Graphics): Unit def move(d: Dimension, delta: Int)...... which must be filled in an actual Point object The Class Spectrum in Scala: Abstract Classes Need a type describing draw, move, collide Finally, we implement a few common methods... abstract class Point { // Can be overridden by concrete Points def collide(d: Dimension) { hitwall(x, 0, d.width - xdim) map {(v:int) => x = v xvel = -xvel... // Cannot be overridden by sub-classes final def draw(g: Graphics) { val c = g.getcolor() // save old color render(g) // render object g.setcolor(c) // restore old color... which must be filled in an actual Point object 10

So, how do we create a real Point?! The Class Spectrum in Scala: (Concrete) Classes The Class Spectrum in Scala: Classes Like an abstract class but with everything defined. class Dot(val name: String, x0: Int, y0: Int) extends Point {... extends Point says Dot is a concrete implementation of Point. The Class Spectrum in Scala: Classes Parameters define several things for free class Dot(val name: String, x0: Int, y0: Int) extends Point { // Filled in by "constructor" var x = x0 var y = y0 def xdim = 20 def ydim = 20... Generated Constructor Initializes fields from parameters x0, y0 Named parameters automatically become fields The Class Spectrum in Scala: Classes We define required methods class Dot(val name: String, x0: Int, y0: Int) extends Point { def render(g: Graphics) { g.filloval(x, y, xdim, ydim) 11

override def move(d: Dimension, delta: Int) { x += (delta * xvel) y += (delta * yvel) Remainder inherited from Point The Class Spectrum in Scala: Classes We have also automatically defined new type... class Dot(val name: String, x0: Int, y0: Int) extends Point { def render(g: Graphics) { g.filloval(x, y, xdim, ydim) override def move(d: Dimension, delta: Int) { x += (delta * xvel) y += (delta * yvel)... with name Dot and specified it is a subtype of Point Called Nominal (by-name) subtyping Can use Dot where Point is expected The Class Spectrum in Scala: Classes Note the auxiliary constructor this Feeling lazy about supplying x0, y0? // Auxiliary constructor, invokes "real" constructor def this(n: String) = this(n, Rand(0, 100), Rand(0, 100)) 12

The Class Spectrum in Scala: Classes Lets create an instance With the default constructor... new Dot("p1", 50, 50)... and with the auxiliary one new Dot("p2") //Uses default constructor Recap: What is a Class? Spectrum from Specification to Implementation 1. Interfaces Only Specification of Types e.g. ticker 2. Abstract Class Specification + Some Concrete Implementations e.g. Point 3. Class Specification + All Concrete Implementations e.g. Dot 13

OK! Lets Run The Screensaver! So far: Objects + Classes + (Some) Inheritance How to get colored dots? Inheritance Extend Dot with a notion of color class ColorDotV1(name: String, color: Color) extends Dot(name) { override def render(g: Graphics) { g.setcolor(color) //"color" in scope from params super.render(g) Note: extends Dot(name) Pithy way of calling the (old) super-class constructor Passing it the parameter(s) of the (new) sub-class Inheritance Enables Reuse Get lots of properties from without any work! Only need to override or extend bits that are different... Lets make another shape How about a Box? class Box(name: String, h: Int, w: Int) extends Dot(name) { override def ydim = w override def xdim = h override def render(g: Graphics) { g.fillrect(x, y, h, w) 14

Get lots of stuff without any work! Get move (from Dot) for free! Get collide (from Point) for free! Lets make another shape Lets make another shape How about a Box? Lets check it out... val points = List(..., new Box("p4", 20, 40) ) But wait a minute... Wait a minute...... must be wary of things you get for free Good Angel: Is a Box really a Dot? Evil Angel: But we re getting so much reuse!! Good Angel:... Lets make (yet) another shape How about we color them Boxes? How? Inheritance? Lets see... 15

Lets make (yet) another shape: ColorBox Lets try extending Box... class ColorBoxV1(name: String, color: Color, h: Int, w: Int) extends Box(name, h: Int, w: Int) { override def render(g: Graphics) { g.setcolor(color) super.render(g)... Yikes! We re repeating ourselves! render is just like ColorDot.render How can we reuse the above render? Lets make (yet) another shape: ColorBox Ok, lets try extending ColorDot...... whats the problem? Problem We bundled Colored-ness into ColorDot...... but color is totally independent of Shape! How can we free up colored-ness from its chains? Traits Traits Describe object behaviors independent particular classes...... Can be mixed into any class to add that behavior to the class! 16

Traits describe Object Behaviors Independently trait Colored extends Point { val color: Color // Will be defined by mixin-target abstract override def render(g: Graphics) { g.setcolor(color) super.render(g) // Call render of mixin-target This exactly specifies and implements colored-ness...... without bundling it inside a particular class! Traits describe Object Behaviors Independently trait Colored extends Point { val color: Color // Will be defined by mixin-target abstract override def render(g: Graphics) { g.setcolor(color) super.render(g) // Call render of mixin-target Wait! Aren t traits just like Java interfaces? Traits Can Be Mixed-Into Appropriate (Host) Classes To use a trait, just mixin to an appropriate host class. (e.g. subtype of Point) class ColorDot(name: String, val color: Color) extends Dot(name) with Colored class ColorBox(name: String, val color: Color, h: Int, w: Int) extends Box(name, h, w) with Colored Lets take the new classes for a spin! 17

Traits: Recap Describe object behaviors independent particular classes...... Can be mixed into any class to add that behavior to the class! Are traits different from Multiple Inheritance Are traits different from Multiple Inheritance Why can t I do something like this: abstract class Point class Dot extends Point {... class Colored extends Point {... and then class ColoredDot extends Dot, Colored {... Wouldn t this solve the problem of reusing Color and Dot? Problem 1: Which parent do I pick method from? Problem 2: Need to compose render from both Are traits different from Multiple Inheritance The cunning thing about traits: trait Colored extends Point { val color: Color // Will be defined by mixin-target abstract override def render(g: Graphics) { g.setcolor(color) super.render(g) // Call render of mixin-target stacks the new behavior on top of existing behavior! (super.render) 18

independent of the host class! So, color-a-box, color-a-dot, color-anything! Another Trait: Bouncing Another Trait: Bouncing Lets add a notion of gravity to the objects... trait Bouncing extends Point {... abstract override def move(d: Dimension, delta: Int) { yvel += delta shootup() // if ROTFL super.move(d, delta) // preserve Another Trait: Bouncing Lets add a gravity behavior to the objects... independent of shape, color, etc. trait Bouncing extends Point {... abstract override def move(d: Dimension, delta: Int) { yvel += delta shootup() // if ROTFL super.move(d, delta) // preserve Note how new behavior stacked on top of old! Reuse old (horizontal) motion via super.move 19

Another Trait: Bouncing We can add bouncing behavior to any Point object class BouncingColorDot(name: String, val color: Color) extends Dot(name) with Colored with Bouncing class BouncingColorBox(name: String, val color: Color, h: Int, w: Int) extends Box(name, h, w) with Colored with Bouncing Or even directly retro-actively to the instance object new Dot("p2") with Bouncing Moral of the story? Moral of the story Inheritance enables reuse BUT Class(ical) inheritance locks reusable behaviors into unrelated classes (e.g. ColorDotV1) In fact... Our code can be cleaned up more... Moral of the story Inheritance enables reuse BUT Class(ical) inheritance locks reusable behaviors into unrelated classes (e.g. ColorDotV1) SO Keep a sharp eye out for independent behaviors, and set them free using traits. 20