Lecture 13: Introduction to inheritance and genericity

Similar documents
Einführung in die Programmierung Introduction to Programming

Lecture 14: More Inheritance & Genericity

Genericity & Inheritance

Eiffel: Analysis, Design and Programming

- 9/10 - Inheritance

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Object-Oriented Software Construction

Lecture 21: Undo/Redo

Assignment 7: Inheritance

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Inheritance and object compatibility

Chapter 5 Object-Oriented Programming

CSE 307: Principles of Programming Languages

What are the characteristics of Object Oriented programming language?

C++ Important Questions with Answers

Containers and genericity. A standardized naming scheme. Cursor properties. Lists. Adding a cell. A specific implementation: (singly) linked lists

Einführung in die Programmierung Introduction to Programming

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Example Test Questions for Inheritance

Universe Type System for Eiffel. Annetta Schaad

Multiple & Repeated! Inheritance!

Software Architecture 4. July 2005

Einführung in die Programmierung Introduction to Programming

Multiple & Repeated Inheritance

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Einführung in die Programmierung Introduction to Programming

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

ECE 3574: Dynamic Polymorphism using Inheritance

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Java Fundamentals (II)

Einführung in die Programmierung Introduction to Programming

Java: introduction to object-oriented features

Conformance. Object-Oriented Programming Spring 2015

Object-Oriented Software Construction

CS558 Programming Languages

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Inheritance. OOP: Inheritance 1

Lecture 8: Control Structures I

Module 10 Inheritance, Virtual Functions, and Polymorphism

Einführung in die Programmierung Introduction to Programming

Inheritance and Polymorphism

Einführung in die Programmierung Introduction to Programming

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CS4215 Programming Language Implementation

ITI Introduction to Computing II

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

ITI Introduction to Computing II

The State Design Pattern

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Inheritance, and Polymorphism.

BON Business Object Notation Based on slides by Prof. Paige

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Fondamenti della Programmazione: Metodi Evoluti. Lezione 7: References and Assignment

Object-oriented Programming. Object-oriented Programming

CS558 Programming Languages

Object typing and subtypes

index.pdf January 21,

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Lecture 16: Object Programming Languages

Java Object Oriented Design. CSC207 Fall 2014

Type Analysis. Type Checking vs. Type Inference

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

2. The object-oriented paradigm

Nature abhors a void. Bertrand Meyer. Software Engineering

Introduction to Programming. Lecture 20: More about inheritance. Multiple inheritance. Composite figures. Bertrand Meyer

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

ITI Introduction to Computing II

10 Feature adaptation

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Programmiersprachen (Programming Languages)

Einführung in die Programmierung Introduction to Programming

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

CSCI-1200 Data Structures Fall 2017 Lecture 25 C++ Inheritance and Polymorphism

Let s briefly review important EER inheritance concepts

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Compilation of Object Oriented Languages Tik Compilers Seminar

Eiffel: An Advanced Introduction. Alan A. Snyder Brian N. Vetter

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

CS153: Compilers Lecture 11: Compiling Objects

ITI Introduction to Computing II

2. The object-oriented paradigm!

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 10

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Einführung in die Programmierung Introduction to Programming

Exercise 8 Parametric polymorphism November 18, 2016

JOURNAL OF OBJECT TECHNOLOGY

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Inheritance and Interfaces

CS-202 Introduction to Object Oriented Programming

Transcription:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 13: Introduction to inheritance and genericity On the menu for today (& next time) Two fundamental mechanisms for expressiveness and reliability: Genericity Inheritance with associated (just as important!) notions: Static typing Polymorphism Dynamic binding 2 Exting the basic notion of class Inheritance Abstraction Type parameterization CITIES SET_OF_ CARS CARS Genericity Type parameterization PERSONS LINKED_LIST_ OF_CARS Specialization 3

Reminder: the dual nature of classes A class is a module A class is a type Or a type template (see genericity) As a module, a class: Groups a set of related services Enforces information hiding (not all services are visible from the outside) Has clients (the modules that use it) and suppliers (the modules it uses) As a type, a class: Denotes possible run-time values (objects & references), the instances of the type Can be used for declarations of entities (representing such values) 4 Inheritance basics Principle: Describe a new class as extension or specialization of an existing class (or several with multiple inheritance) If B inherits from A : As modules: all the services of A are available in B (possibly with a different implementation). As types: whenever an instance of A is required, an instance of B will be acceptable ( is-a relationship) 5 Terminology Parent, Heir Ancestor, Descant The ancestors of B are B itself and the ancestors of its parents. Proper ancestor, Proper descant Direct instance, Instance The instances of A are the direct instances of its descants. A B features features (Other terminology: subclass, superclass, base class) 6

Terminology If B inherits from A (by listing A in its inherit clause), B is an heir of A and A is a parent of B. The descants of a class are the class itself and (recursively) the descants of its heirs; proper descants exclude the class itself. Ancestor and proper ancestor are the reverse notions. 7 Example hierarchy deferred + effective ++ redefined MOVING position update_coordinates move VEHICLE load take busy TAXI LINE_VEHICLE update_coordinates++ move++ take+ EVENT_TAXI TRAM BUS 8 Features Feature (for the examples): From class: take (from_location, to_location: COORDINATE) -- Bring passengers from from_location -- to to_location. busy : BOOLEAN --Is taxi busy? load (q : INTEGER) -- Load q passengers. position : TRAFFIC_COORDINATE -- Current position on map EVENT_TAXI TAXI VEHICLE MOVING 9

Inheriting features class EVENT_TAXI inherit TAXI feature [ Rest of class ] deferred class TAXI inherit VEHICLE feature [ Rest of class ] deferred class VEHICLE inherit MOVING feature [ Rest of class ] All features of TAXI are applicable to instances of EVENT_TAXI All features of VEHICLE are applicable to instances of TAXI All features of MOVING are applicable to instances of VEHICLE 10 Inheritance hierarchy deferred + effective ++ redefined MOVING position update_coordinates move VEHICLE load take busy TAXI LINE_VEHICLE update_coordinates++ move++ take+ EVENT_TAXI TRAM BUS 11 Inherited features m : MOVING, v : VEHICLE, t : TAXI, e : EVENT_TAXI v load ( ) e take ( ) m position t busy e load ( ) e take ( ) e position e busy -- An expression -- An expression -- An expression -- An expression 12

Definitions: kinds of features A feature of a class is one of: An inherited feature if it is a feature of one of the parents of the class. An immediate feature if it is declared in the class, and not inherited. In this case the class is said to introduce the feature. 13 Polymorphic assignment v : VEHICLE cab: EVENT_TAXI v := cab A proper descant type v cab BEFORE AFTER (VEHICLE) (EVENT_TAXI) 14 Assignments So far (no polymorphic assignments): target := expression where expression is of the same type as target. With polymorphism: target := expression where the type of expression is a descant of the type of target. 15

Polymorphism is also for argument passing register_trip (v: VEHICLE ) register_trip (cab) A proper descant type 16 Definitions: Polymorphism An attachment (assignment or argument passing) is polymorphic if its target variable and source expression have different types. An entity or expression is polymorphic if it may at runtime as a result of polymorphic attachments become attached to objects of different types. Polymorphism is the existence of these possibilities. 17 Definitions (Static and dynamic type) The static type of an entity is the type used in its declaration in the corresponding class text. If the value of the entity, during a particular execution, is attached to an object, the type of that object is the entity s dynamic type. 18

Static and dynamic type v: VEHICLE cab: EVENT_TAXI v := cab Static type: VEHICLE Dynamic type after this assignment: EVENT_TAXI For a polymorphic attachment to be valid, the type of the source must conform to the type of the target. 19 Definition: Conformance A (non-generic) reference type U conforms to a reference type T if U is a descant of T. An expanded type conforms only to itself. 20 Another example hierarchy center FIGURE display rotate OPEN_ FIGURE perimeter CLOSED_ FIGURE perimeter + perimeter + + + SEGMENT POLYLINE POLYGON ELLIPSE... TRIANGLE diagonal perimeter ++... side1 RECTANGLE side2 deferred + effective perimeter ++ SQUARE CIRCLE perimeter ++ ++ redefined 21

Redefinition 1: polygons class POLYGON inherit CLOSED_FIGURE create make feature vertex : ARRAY [POINT] vertex_count : INTEGER perimeter : REAL is -- Perimeter length do from... until... loop vertex [i ] vertex [i + 1] Result := Result + vertex [i ]. distance (vertex [i + 1])... invariant vertex_count >= 3 vertex_count = vertex.count 22 Redefinition 2: rectangles class RECTANGLE inherit POLYGON redefine perimeter create make feature diagonal, side1, side2 : REAL perimeter : REAL is -- Perimeter length do Result := 2 (side1 + side2) invariant diagonal side1 side2 vertex_count = 4 23 Inheritance, typing and polymorphism Assume: p : POLYGON ; r : RECTANGLE ; t : TRIANGLE; x : REAL Permitted: x := p.perimeter x := r.perimeter x := r.diagonal p := r NOT permitted: x := p.diagonal -- Even just after p := r! r := p p r (POLYGON) (RECTANGLE) 24

Dynamic binding What is the effect of the following (assuming some_test true)? if some_test then p := r else p := t x := p.perimeter Redefinition: A class may change an inherited feature, as with POLYGON redefining perimeter. Polymorphism: p may have different forms at run-time. Dynamic binding: Effect of p.perimeter deps on run-time form of p. 25 Without dynamic binding! display (f : FIGURE ) is do if f is a CIRCLE then... elseif f is a POLYGON then... and similarly for all other routines! Tedious; must be changed whenever there s a new figure type. 26 With inheritance and associated techniques With: f : FIGURE c : CIRCLE p : POLYGON and: create c.make (...) create p.make (...) Initialize: if... then f := c else f := p Then just use: f.move (...) f.rotate (...) f.display (...) -- and so on for every -- operation on f! 27

Inheritance: summary 1 Type mechanism: lets you organize our data abstractions into taxonomies Module mechanism: lets you build new classes as extensions of existing ones Polymorphism: Flexibility with type safety Dynamic binding: automatic adaptation of operation to target, for more modular software architectures 28 Redefinition deferred class MOVING feature origin : COORDINATE destination : COORDINATE position : COORDINATE polycursor : ARRAYED_LIST_CURSOR [COORDINATE] update_coordinates -- Update origin and destination. [ ] do [ ] origin := destination polycursor.forth destination := polycursor.item [ ] polycursor.i_th(i) polycursor.i_th (i + 1) 29 Redefinition 2: LINE_VEHICLE deferred class LINE_VEHICLE inherit VEHICLE polycursor.i_th(i) redefine update_coordinates feature linecursor: LINE_CURSOR update_coordinates is polycursor.i_th (i + 1) -- Update origin and destination. do [ ] origin := destination polycursor.forth if polycursor.after then linecursor.forth create polycursor.make (linecursor.item.polypoints) polycursor.start destination := polycursor.item 30

Dynamic binding What is the effect of the following (assuming some_test true)? m: MOVING, l: LINE_VEHICLE, t: TAXI if some_test then m := l else m := t m.update_coordinates Redefinition: A class may change an inherited feature, as with LINE_VEHICLE redefining update_coordinates. Polymorphism: m may have different forms at run-time. Dynamic binding: Effect of m.update_coordinates deps on run-time form of m. 31 Dynamic binding There are multiple versions of take. Inherits from Deferred busy take TAXI take EVENT_TAXI DISPATCHER_TAXI take 32 Definitions (Dynamic binding) Dynamic binding (a semantic rule) is the property that any execution of a feature call will use the version of the feature best adapted to the type of the target object. 33

Exting the basic notion of class Abstraction Inheritance Type parameterization CITIES SET_OF_ CARS CARS Genericity Type parameterization PERSONS LINKED_LIST_ OF_CARS Specialization 34 Exting the basic notion of class Inheritance SET_OF_ CARS SET_OF_ PERSONS Genericity CITIES CARS PERSONS LINKED_LIST_ OF_CITIES LINKED_LIST_ OF_CARS 35 Genericity Unconstrained LIST [G] e.g. LIST [INTEGER], LIST [PERSON] Constrained HASH_TABLE [G > HASHABLE] VECTOR [G > NUMERIC] 36

Genericity: Ensuring type safety How can we define consistent container data structures, e.g. list of accounts, list of points? Dubious use of a container data structure: c : CITY ; p : PERSON cities : LIST... people : LIST... --------------------------------------------------------- people.ext p ( ) cities.ext c ( ) c := cities.last c. trams.force (Line8, Line8.name) What if wrong? 37 Possible approaches 1. Duplicate code, manually or with help of macro processor. 2. Wait until run time; if types don t match, trigger a runtime failure. (Smalltalk) 3. Convert ( cast ) all values to a universal type, such as pointer to void in C. 4. Parameterize the class, giving an explicit name G to the type of container elements. This is the Eiffel approach, now also found in Java,.NET and others. 38 A generic class Formal generic parameter class LIST [G ] feature ext (x : G ) is... last : G is... To use the class: obtain a generic derivation, e.g. cities: LIST [CITY ] Actual generic parameter 39

Using generic derivations cities : LIST [CITY ] people : LIST [PERSON] c : CITY p : PERSON... cities.ext (c) people.ext (p) STATIC TYPING: The compiler will reject: people.ext (c) cities.ext (p) c := cities.last c.lines.force (Line8, Line8.name) 40 Static typing Type-safe call (during execution): A feature call x.f such that the object attached to x has a feature corresponding to f. [Generalizes to calls with arguments, x.f (a, b) ] Static type checker: A program-processing tool (such as a compiler) that guarantees, for any program it accepts, that any call in any execution will be type-safe. Statically typed language: A programming language for which it is possible to write a static type checker. 41 Using genericity LIST [CITY ] LIST [LIST [CITY ]] A type is no longer exactly the same thing as a class! (But every type remains based on a class.) 42

Polymorphic data structures fleet: LIST [VEHICLE] v: VEHICLE ext (v : G) -- Add a new occurrence of v. fleet.ext (v) fleet.ext (cab) (TAXI) (TRAM) (TRAM) (BUS) (TAXI) 43 Definition (Polymorphism, adapted) An attachment (assignment or argument passing) is polymorphic if its target entity and source expression have different types. An entity or expression is polymorphic if as a result of polymorphic attachments it may at runtime become attached to objects of different types. A container data structure is polymorphic if it may contain references to objects of different types. Polymorphism is the existence of these possibilities. 44 Definitions (Conformance, adapted) A reference type U conforms to a reference type T if either: They have no generic parameters, and U is a descant of T. They are both generic derivations with the same number of actual generic parameters, the base class of U is a descant of the base class of T, and every actual parameter of U (recursively) conforms to the corresponding actual parameter of T. An expanded type conforms only to itself. 45

What we have seen The basics of fundamental O-O mechanisms: Inheritance Polymorphism Dynamic binding Static typing Genericity 46 End of lecture 13 47