Lecture 21: Inheritance CS200: Computer Science University of Virginia

Slides:



Advertisements
Similar presentations
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
Advertisements

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.
Lecture 24: Gödel’s Proof CS150: Computer Science
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
Fractions, Decimals, and Percent By: Jack M 1 _____ %
CS 403 – Programming Languages Class 25 November 28, 2000.
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 Lecture 3: Rules of Evaluation.
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)
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 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.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
Complex Numbers warm up 4 Solve the following Complex Numbers Any number in form a+bi, where a and b are real numbers and i is imaginary. What is an.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 22: Objects I invented the term Object-
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Lecture 3: Rules of Evaluation CS150: Computer Science
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 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.
Object Oriented Programming in Java Habib Rostami Lecture 2.
Introduction to Complex Numbers Adding, Subtracting, Multiplying And Dividing Complex Numbers SPI Describe any number in the complex number system.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 21: Inheritance.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
Object Oriented Programming Systems
(Thunking about Thunks)
Object-Oriented Programming
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Lecture 6: Lambda Calculus
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
6.001 SICP Object Oriented Programming
Class 22: Inheritance CS150: Computer Science University of Virginia
Fractions: Adding and Subtracting Like Denominators
Lecture 7: List Recursion CS200: Computer Science
The role of abstractions
Lambda Calculus Revisited
Types of Programming Languages
Class 23: Gödel’s Theorem CS150: Computer Science
Building Java Programs
Representing Structure and Behavior with Trees
Lecture 11: All Sorts CS200: Computer Science University of Virginia
Lecture 28: Types of Types
Fractions: Adding and Subtracting Like Denominators
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
topics three representations entity relationship diagram object model
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Lecture 10: Using Object-Oriented Languages
Homework Help.
Comp1202: Inheritance I Super and Sub-classes.
Review of Previous Lesson
CS2013 Lecture 7 John Hurley Cal State LA.
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 11: Sorting Grounds and Bubbles
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

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

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

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

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

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

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

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

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

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

Inheritance 8 March 2002 CS 200 Spring 2002

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

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

> (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

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

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

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

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

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

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

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.

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

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

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

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 http://java.sun.com/products/java-media/3D/collateral/j3dclass.html 8 March 2002 CS 200 Spring 2002

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