Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 21: Inheritance CS200: Computer Science University of Virginia

Similar presentations


Presentation on theme: "Lecture 21: Inheritance CS200: Computer Science University of Virginia"— Presentation transcript:

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

2 Menu Objects Review Inheritance PS6 8 March 2002 CS 200 Spring 2002

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

4 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.) 8 March 2002 CS 200 Spring 2002

5 ask (define (ask object message) (object message))
Lecture 20: (define (ask object message) (object message)) (define (ask object message . args) (apply (object message) object args)) 8 March 2002 CS 200 Spring 2002

6 3 > (define san (make-number 3)) > (ask san 'value) 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)))))))) global environment + : #<primitive:+> make-number: san: > (define san (make-number 3)) > (ask san 'value) 3 > (ask san 'add (make-number 4)) 7 3 n : parameters: body: ((lambda … parameters: message body: (cond ((eq? … 8 March 2002 CS 200 Spring 2002

7 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 8 March 2002 CS 200 Spring 2002

8 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 8 March 2002 CS 200 Spring 2002

9 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.) 8 March 2002 CS 200 Spring 2002

10 Inheritance 8 March 2002 CS 200 Spring 2002

11 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)))))) 8 March 2002 CS 200 Spring 2002

12 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 8 March 2002 CS 200 Spring 2002

13 > (ask half 'add half) | (eq? add value) | #f
> (trace ask) > (trace eq?) > (ask half 'add half) |(ask #<procedure> add #<procedure>) | (eq? add value) | #f | (eq? add get-denominator) | (eq? add get-numerator) | (eq? add add) | #t | (ask #<procedure> value) | |(eq? value value) | |#t | 1/2 |1 1 8 March 2002 CS 200 Spring 2002

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

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

16 English C++ Java Eiffel Beta Smalltalk (72)
A Fraction is a special 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! Number Fraction Note: people draw this different ways (some times the arrow points the opposite way) 8 March 2002 CS 200 Spring 2002

17 CS 200 Fraction inherits from Number.
Fraction is a subclass of Number. The superclass of Fraction is Number. Number Fraction Note: people draw this different ways (some times the arrow points the opposite way) 8 March 2002 CS 200 Spring 2002

18 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 We will cover types and subtyping after Spring Break, just warning you now 8 March 2002 CS 200 Spring 2002

19 PS6 Make an adventure game programming with objects.
8 March 2002 CS 200 Spring 2002

20 PS6 Classes object physical-object place mobile-object thing person
make-class is the procedure for constructing objects in the class class mobile-object thing person student police-officer make-student constructs a student. The student class inherits from person, which inherits from mobile-object, which inherits from physical-object, which inherits from object.

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

22 Are there class hierarchies like this in the real world or just in fictional worlds like Charlatansville? 8 March 2002 CS 200 Spring 2002

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

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

25 Charge PS6 Spring Break Programming with Objects
Read rest of GEB, Part I Reading questions on the Notes today 8 March 2002 CS 200 Spring 2002


Download ppt "Lecture 21: Inheritance CS200: Computer Science University of Virginia"

Similar presentations


Ads by Google