David Evans CS200: Computer Science University of Virginia Computer Science Lecture 21: Inheritance.

Slides:



Advertisements
Similar presentations
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
Lecture 24: Gödel’s Proof CS150: Computer Science
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
BCS 2143 Introduction to Object Oriented and Software Development.
CS 403 – Programming Languages Class 25 November 28, 2000.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 38: Fixed Points and Biological Computing.
David Evans CS150: Computer Science University of Virginia Computer Science Class 33: Computing with Photons From The.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 20: Objects I invented the term Object-
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Class 25: Python, Objects, Bombs, and Inheritance University of Virginia cs1120 David Evans.
Multiplying fractions by whole numbers 1.What are 4 lots of one quarter? 2.What are 10 lots of one half? x x x 3 6.½ x 7 7.¼ x 6.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 23: Programming with Objects.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
ISBN Object-Oriented Programming Chapter Chapter
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 22: Objects I invented the term Object-
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
Lecture 3: Rules of Evaluation CS150: Computer Science
Unified Modeling Language (UML)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Intractable Problems (Smiley Puzzles.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 4: Metacircles Eval Apply David Evans
CSC 221: Computer Programming I Spring 2010
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 22: Inheritance CS150: Computer Science University of Virginia
Behavioral Design Patterns
CSC 221: Computer Programming I Fall 2005
Types of Programming Languages
Class 23: Gödel’s Theorem CS150: Computer Science
Representing Structure and Behavior with Trees
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 13 - Assignment and the environments model Chapter 3
Homework Help.
Comp1202: Inheritance I Super and Sub-classes.
Multiplying fraction and whole number
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 21: Inheritance

17 March 2003CS 200 Spring Menu Objects Review Inheritance PS5: Databases PS6

17 March 2003CS 200 Spring Objects When we package state and procedures together we have an object Programming with objects is object-oriented programming

17 March 2003CS 200 Spring make-number (define make-number (lambda (n) (lambda (message) (cond ((eq? message 'value) (lambda (self) n)) ((eq? message 'add) (lambda (self other) (+ (ask self 'value) (ask other 'value)))))))) Why don’t we just use n ? (Well see why later today.)

17 March 2003CS 200 Spring ask (define (ask object message. args) (apply (object message) object args)) (define (ask object message) (object message)) Lecture 20:

17 March 2003CS 200 Spring global environment + : # make-number: parameters: body: ((lambda … n : 3 (define make-number (lambda (n) (lambda (message) (cond ((eq? message 'value) (lambda (self) n)) ((eq? message 'add) (lambda (self other) (+ (ask self 'value) (ask other 'value)))))))) san: parameters: message body: (cond ((eq? … > (define san (make-number 3)) > (ask san 'value) 3 > (ask san 'add (make-number 4)) 7

17 March 2003CS 200 Spring There are many kinds of numbers… Whole Numbers (0, 1, 2, …) Integers (-23, 73, 0, …) Fractions (1/2, 7/8, …) Floating Point (2.3, , ) But they can’t all do the same things –We can get the denominator of a fraction, but not of an integer

17 March 2003CS 200 Spring make-fraction (define make-fraction (lambda (numerator denominator) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numerator denominator)) ((eq? message 'add) (lambda (self other) (+ (ask self 'value) (ask other 'value))) ((eq? message ‘get-numerator) (lambda (self) numerator)) ((eq? message ‘get-denominator) (lambda (self) denominator)) ))))) Same as in make-number

17 March 2003CS 200 Spring Why is redefining add a bad thing? Cut-and-paste is easy but… There could be lots of number methods (subtract, multiply, print, etc.) Making the code bigger makes it harder to understand If we fix a problem in the number add method, we have to remember to fix the copy in make-fraction also (and real, complex, float, etc.)

17 March 2003CS 200 Spring Inheritance

17 March 2003CS 200 Spring make-fraction (define (make-fraction numer denom) (let ((super (make-number #f))) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numer denom))) ((eq? message 'get-denominator) (lambda (self) denom)) ((eq? message 'get-numerator) (lambda (self) numer)) (else (super message))))))

17 March 2003CS 200 Spring Using Fractions > (define half (make-fraction 1 2)) > (ask half 'value) 1/2 > (ask half 'get-denominator) 2 > (ask half 'add (make-number 1)) 3/2 > (ask half 'add half) 1

17 March 2003CS 200 Spring > (trace ask) > (trace eq?) > (ask half 'add half) |(ask # add # ) | (eq? add value) | #f | (eq? add get-denominator) | #f | (eq? add get-numerator) | #f | (eq? add value) | #f | (eq? add add) | #t | (ask # value) | |(eq? value value) | |#t | 1/2 | (ask # value) | |(eq? value value) | |#t | 1/2 |1 1

17 March 2003CS 200 Spring > (trace ask) > (trace eq?) > (ask half 'add half) |(ask # add # ) | (eq? add value) | #f | (eq? add get-denominator) | #f | (eq? add get-numerator) | #f | (eq? add value) | #f | (eq? add add) | #t | (ask # value) | |(eq? value value) | |#t | 1/2 | (ask # value) | |(eq? value value) | |#t | 1/2 |1 1 make-number make-fraction

17 March 2003CS 200 Spring Inheritance Inheritance is using the definition of one class to make another class make-fraction uses make-number to inherit the behaviors of number

17 March 2003CS 200 Spring Number Fraction Note: people sometimes draw this different ways English A Fraction is a kind of Number. C++ Fraction is a derived class whose base class is Number Java Fraction extends Number. Eiffel Fraction inherits from Number. Beta Fraction is a subpattern of Number. Smalltalk (72) Didn’t have inheritance!

17 March 2003CS 200 Spring CS 200: Fraction inherits from Number. Fraction is a subclass of Number. The superclass of Fraction is Number. Number Fraction

17 March 2003CS 200 Spring Inheritance and Subtyping Inheritance: reusing the implementation of one object to make a new kind of object Often confused with subtyping which is saying one kind of object can be used where another kind of object is expected –CS200 won’t cover subtyping (take CS201J) –We will cover types (Class 29)

17 March 2003CS 200 Spring PS5 How are commercial databases different from what you implemented for PS5? UVa’s Integrated Systems Project to convert all University information systems to use an Oracle database was originally budgeted for $58.2 Million (starting in 1999). Actual cost is likely to be $100 Million.

17 March 2003CS 200 Spring Real Databases Atomic Transactions: a transaction may involve many modifications to database tables, but the changes should only happen if the whole transaction happens (e.g., don’t charge the credit card unless the order is sent to the shipping dept) Security: limit read/write access to tables, entries and fields Storage: need to efficiently store data on disk, provide backup mechanisms Scale: to support really big data tables, real databases do lots of clever things

17 March 2003CS 200 Spring How big are big databases? Microsoft TerraServer –Claimed biggest in 1998 –Aerial photos of entire US (1 meter resolution)

17 March 2003CS 200 Spring You are here Rotunda Amphitheater

17 March 2003CS 200 Spring AFC? Picture from 2 Apr 1994 You are here

17 March 2003CS 200 Spring Big Databases Microsoft TerraServer –3.3 Terabytes (claimed biggest in 1998) –1 Terabyte = 2 40 Bytes ~ 1 Trillion Bytes Wal-Mart –285 Terabytes Stanford Linear Accelerator (BaBar) –500 Terabytes (30 KB per particle collision)

17 March 2003CS 200 Spring PS6 Make an adventure game programming with objects

17 March 2003CS 200 Spring PS6 Classes object physical-object place mobile-object thing person student police-officer make-class is the procedure for constructing objects in the class class student inherits from person which inherits from mobile-object which inherits from physical-object which inherits from object.

PS6 Objects object physical-object place mobile-object thing person student police-officer Cabal Hall Recursa Alyssa P. Hacker (make-place name) evaluates to an object that is an instance of the class place.

17 March 2003CS 200 Spring Are there class hierarchies like this in the real world or just in fictional worlds like Charlottansville?

17 March 2003CS 200 Spring Microsoft Foundation Classes CButton inherits from CWnd inherits from CObject “A button is a kind of window is a kind of object”

17 March 2003CS 200 Spring Java 3D Class Hierarchy Diagram RotationPathInterpolator PathInterpolator Interpolator Selector Node Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this!

17 March 2003CS 200 Spring Charge PS5 –Return now, comments will be available Wednesday PS6 –Programming with Objects Wednesday and Friday: –How will solving the (generalized) Cracker Barrel Peg Board puzzle help cure cancer?