Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance

2 17 March 2004CS 200 Spring 20032 Menu Objects Review Inheritance PS6

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

4 17 March 2004CS 200 Spring 20034 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.)

5 17 March 2004CS 200 Spring 20035 ask (define (ask object message. args) (apply (object message) object args)) (define (ask object message) (object message)) Lecture 22:

6 17 March 2004CS 200 Spring 20036 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

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

8 17 March 2004CS 200 Spring 20038 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 Note: our add method evaluates to a number, not a fraction object (which would be better).

9 17 March 2004CS 200 Spring 20039 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.)

10 17 March 2004CS 200 Spring 200310 Inheritance

11 17 March 2004CS 200 Spring 200311 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))))))

12 17 March 2004CS 200 Spring 200312 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

13 17 March 2004CS 200 Spring 200313 > (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

14 17 March 2004CS 200 Spring 200314 > (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

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

16 17 March 2004CS 200 Spring 200316 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 17 March 2004CS 200 Spring 200317 CS 200: Fraction inherits from Number. Fraction is a subclass of Number. The superclass of Fraction is Number. Number Fraction

18 17 March 2004CS 200 Spring 200318 Inheritance and Subtyping Inheritance: reusing the definition of one class to make a new kind of class make-fraction uses make-number “fraction inherits from number” Often confused with subtyping which is saying one kind of object can be used where another kind of object is expected

19 17 March 2004CS 200 Spring 200319 Subtyping Subtyping is very important in statically typed languages (like C, C++, C#, Java, Pascal) where you have to explicitly declare a type for all variables: method Number add (Number n) { … } CS200 won’t cover subtyping (although we will talk more about types later) Because of subtyping, either a Number or a Fraction (subtype of Number) could be passed as the argument

20 17 March 2004CS 200 Spring 200320 PS6 Make an adventure game programming with objects Many objects in our game have similar properties and behaviors, so we use inheritance.

21 17 March 2004CS 200 Spring 200321 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.

22 17 March 2004CS 200 Spring 200322 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.

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

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

25 17 March 2004CS 200 Spring 200325 Java 3D Class Hierarchy Diagram http://java.sun.com/products/java-media/3D/collateral/j3dclass.html RotationPathInterpolator PathInterpolator Interpolator Behavior Node Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this! Object

26 17 March 2004CS 200 Spring 200326 Charge PS6 –Programming with Objects –Due Date change: Previously Monday, now due Friday 26 March Or, turn in Monday without doing questions 7 and 8 Friday: Is there anything that cannot be computed?


Download ppt "David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance."

Similar presentations


Ads by Google