1 / 25

Inheritance in Object-Oriented Programming

Learn about inheritance in object-oriented programming, its benefits, and why it is important. Explore examples and understand how it allows for code reusability and organization.

ninabowman
Download Presentation

Inheritance in Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Menu • Objects Review • Inheritance • PS6 CS 200 Spring 2002

  3. Objects • When we package state and procedures together we have an object • Programming with objects is object-oriented programming 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.) CS 200 Spring 2002

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

  6. (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? … 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, 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 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 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.) CS 200 Spring 2002

  10. Inheritance 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)))))) 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 CS 200 Spring 2002

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

  16. Number • English 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! Fraction Note: people draw this different ways (some times the arrow points the opposite way) CS 200 Spring 2002

  17. Number • CS 200 Fraction inherits from Number. Fraction is a subclass of Number. The superclass of Fraction is Number. Fraction Note: people draw this different ways (some times the arrow points the opposite way) 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 CS 200 Spring 2002

  19. PS6Make an adventure game programming with objects. CS 200 Spring 2002

  20. object PS6 Classes physical-object place 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. object PS6 Objects physical-object place Cabal Hall mobile-object Recursa (make-place name) evaluates to an object that is an instance of the class place. thing person student police-officer Officer Krispy Alyssa P. Hacker

  22. Are there class hierarchies like this in the real world or just in fictional worlds like Charlatansville? 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” CS 200 Spring 2002

  24. RotationPathInterpolator PathInterpolator Interpolator Node Selector Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this! Java 3D Class Hierarchy Diagram http://java.sun.com/products/java-media/3D/collateral/j3dclass.html CS 200 Spring 2002

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

More Related