Class 22: Inheritance

Similar documents
Class 30: Language Construction

Fall 2010 UVa David Evans. Yixin Sun

So far, we have seen two different approaches for using computing to solve problems:

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

Object-Oriented Technology. Rick Mercer

Objects. object-oriented. programming.

Object-Oriented Programming and Design

CS558 Programming Languages

Simula 67. Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula

Chapter 9 :: Data Abstraction and Object Orientation

Chapter 13. Object Oriented Programming

LATENT METHODS. Richard CS

6.1 Evaluate Roots and Rational Exponents

Chapter 10 :: Data Abstraction and Object Orientation

Rules and syntax for inheritance. The boring stuff

Implementing Interpreters. Who was the first objectoriented. programmer? Objects & Python Interpreters. One-Slide Summary. Outline

CSCI.4430/6969 Programming Languages Lecture Notes

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

Fraction Arithmetic. A proper fraction is a fraction with a smaller numerator than denominator.

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS558 Programming Languages

CS 1124 Media Computation. Steve Harrison Lecture 1.2 (August 27, 2008)

Lecture 15: Object-Oriented Programming

Lecture Notes on Programming Languages

Com S 541. Programming Languages I

References: Chapters 10 and 11 Chapters 8, and 12( 2 and 3)

Inheritance and Substitution (Budd chapter 8, 10)

CS558 Programming Languages Winter 2013 Lecture 8

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Lecturer: William W.Y. Hsu. Programming Languages

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Dynamic Object-Oriented Programming with Smalltalk 1. Introduction

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

CS1 Recitation. Week 1

Introduction to Computers and Programming Languages. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

6.001 Notes: Section 6.1

Back to ObjectLand. Contents at: Chapter 5. Questions of Interest. encapsulation. polymorphism. inheritance overriding inheritance super

Midterms Save the Dates!

CS153: Compilers Lecture 11: Compiling Objects

Table of Laplace Transforms

6.001 Notes: Section 4.1

Intro. Scheme Basics. scm> 5 5. scm>

Lecture 16: Object Programming Languages

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Object typing and subtypes

MA 1128: Lecture 02 1/22/2018

The Rise of OOP: Part 1 [The Early Years] Dina Lamdany

Lecture 23: Object Lifetime and Garbage Collection

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Announcements. Java Review. More Announcements. Today. Assembly Language. Machine Language

Introduction to Scientific Computing Lecture 1

Programming for Mobile Computing

Data Abstraction. Hwansoo Han

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

The Bugs and the Bees

Types, Expressions, and States

How convincing is our Halting Problem proof? Lecture 36: Modeling Computing. Solutions. DrScheme. What is a model? Modeling Computation

Subtyping (Dynamic Polymorphism)

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

An Introduction to Subtyping

Object Oriented Programming

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Asking For Help. BIT 115: Introduction To Programming 1

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

A Brief History of Computer Science

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula. John Mitchell

Inheritance and Interfaces

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

(Refer Slide Time: 4:00)

Design Patterns: State, Bridge, Visitor

6.001 Notes: Section 1.1

Problem One: A Quick Algebra Review

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

CS558 Programming Languages

VARIABLES AND TYPES CITS1001

Is it ever useful to be confident that a problem is hard?

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Programming via Java Defining classes

Object Oriented Programming (OOP)

CS 2110 AEW SIGN UP FOR 1 CREDIT S/U COURSE!!!

Organization of User Interface Software

CS 231 Data Structures and Algorithms, Fall 2016

CS 2110 AEW. Machine Language. Outline. Assembly Language. High-Level Language 1/26/2012

PROGRAMMING LANGUAGE PARADIGMS & THE MAIN PRINCIPLES OF OBJECT-ORIENTED PROGRAMMING

Midterms Save the Dates!

Describe the layers of a computer system

Defining Recursive Procedures. Lecture 8: Recursing Lists. list? Tracing list? sumlist. Defining Recursive Procedures on Lists

Menu. In general, what are possible advantages of one design over another? Class 27: Exam 2. Exam 2 Parenthesizing the Expression. Design A.

Ch. 11: References & the Copy-Constructor. - continued -

Programming Paradigms

Designing and Implementing Classes Topic 6

1 Addition and Subtraction

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Transcription:

Menu Class 22: Inheritance Objects Review Object-Oriented Programming Inheritance CS50: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans 2 Objects When we package state and procedures together we have an object Programming with objects is object-oriented programming Counter in Scheme (define (make-ocounter) ((lambda (count) (if (eq? message 'reset) (set! count 0) (if (eq? message 'next) (set! count (+ count)) (if (eq? message 'how-many) count))))) 0)) 3 4 Counter in Scheme using let (define (make-ocounter) (let ((count 0)) (if (eq? message 'reset) (set! count 0) (if (eq? message 'next) (set! count (+ count)) (if (eq? message 'how-many) count)))))) Defining ask (ask Object Method) > (ask bcounter 'how-many) 0 > (ask bcounter 'next) > (ask bcounter 'how-many) (define (ask object message) (object message)) 5 6

make-number (define make-number (lambda (n) (lambda (self) n)) ((eq? message 'add) Why don t we just use n? (Well see why later today.) (lambda (self other) (+ (ask self 'value) (ask other 'value)))))))) ask with arguments (define (ask object message) (object message)) The. means take all the rest of the parameters and make them into a list. (define (ask object message. args) (apply (object message) object args)) 7 8 (define make-number (lambda (n) (lambda (self) n)) ((eq? message 'add) (lambda (self other) (+ (ask self 'value) (ask other 'value)))))))) > (define san (make-number 3)) > (ask san 'value) 3 > (ask san 'add (make-number 4)) 7 global environment n : 3 + : #<primitive:+> san: make-number: parameters: body: ((lambda parameters: message body: ((eq? Object-Oriented Programming 9 0 Simula Considered the first object-oriented programming language Language designed for simulation by Kristen Nygaard and Ole-Johan Dahl (Norway, 962) Had special syntax for defining classes that packages state and procedures together Counter in Simula class counter; integer count; begin procedure reset(); count := 0; end; procedure next(); count := count + ; end; integer procedure how-many(); how-many := count; end; end 2 2

XEROX Palo Alto Research Center (PARC) 970s: Bitmapped display Graphical User Interface Steve Jobs paid $M to visit and PARC, and returned to make Apple Lisa/Mac Ethernet First personal computer (Alto) PostScript Printers Object-Oriented Programming Dynabook, 972 (Just a model) Don t worry about what anybody else is going to do The best way to predict the future is to invent it. Really smart people with reasonable funding can do just about anything that doesn't violate too many of Newton's Laws! Alan Kay, 97 3 4 Dynabook 972 Tablet computer Intended as tool for learning Kay wanted children to be able to program it also Hallway argument, Kay claims you could define the most powerful language in the world in a page of code Proof: Smalltalk Scheme is as powerful, but takes two pages BYTE Magazine, August 98 5 6 Smalltalk Everything is an object Objects communicate by sending and receiving messages Objects have their own state (which may contain other objects) How do you do 3 + 4? Counter in Smalltalk class name counter instance variable names count new count <- 0 next count <- count + how-many ^ count send the object 3 the message + 4 7 8 3

There are many kinds of numbers Inheritance Whole Numbers (0,, 2, ) Integers (-23, 73, 0, ) Fractions (/2, 7/8, ) Floating Point (2.3, 0.0004, 3.459) But they can t all do the same things We can get the denominator of a fraction, but not of an integer 9 20 make-fraction (define make-fraction (lambda (numerator denominator) (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). 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.) 2 22 make-fraction (define (make-fraction numer denom) (let ((super (make-number #f))) (lambda (self) (/ numer denom))) ((eq? message 'get-denominator) (lambda (self) denom)) ((eq? message 'get-numerator) (lambda (self) numer)) (else (super message)))))) Using Fractions > (define half (make-fraction 2)) > (ask half 'value) /2 > (ask half 'get-denominator) 2 > (ask half 'add (make-number )) 3/2 > (ask half 'add half) 23 24 4

> (trace ask) > (trace eq?) > (ask half 'add half) (ask #<procedure> add #<procedure>) (eq? add get-denominator) (eq? add get-numerator) (eq? add add) #t /2 /2 > (trace ask) > (trace eq?) > (ask half 'add half) (ask #<procedure> add #<procedure>) (eq? add get-denominator) (eq? add get-numerator) (eq? add add) #t make-number make-fraction /2 /2 25 26 Inheritance Inheritance is using the definition of one class to make another class make-fraction uses make-number to inheri the behaviors of number 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) (and Squeak 05) Don t have inheritance! 27 28 Number Fraction CS 50: Fraction inherits from Number. Fraction is a subclass of Number. The superclass of Fraction is Number. 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) { } Because of subtyping, either a Number or a Fraction (subtype of Number) could be passed as the argument We won t cover subtyping (although we will talk more about types later) 29 30 5

Who was the first object-oriented programmer? By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe. Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations, and which should be also susceptible of adaptations to the action of the operating notation and mechanism of the engine... Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations, the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent. Ada, Countess of Lovelace, around 830 3 32 Charge PS5: Due Monday PS6: Out Monday Programming an adventure game using objects and inheritance 33 6