Einführung in die Programmierung Introduction to Programming Prof. Dr

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Written by: Dr. JJ Shepherd
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Principles of Object-Oriented Software Development The language Eiffel.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
Generics and Type Safety Recitation – 4/24/2009 CS 180 Department of Computer Science, Purdue University.
Chair of Software Engineering Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Eiffel: Analysis, Design and Programming Bertrand Meyer Chair of Software Engineering.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 10 M. Piccioni 24/25 November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Java Generics.
Sixth Lecture ArrayList Abstract Class and Interface
Ranga Rodrigo Based on Marc Priestley's Lectures
Inheritance ITI1121 Nour El Kadri.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Generics, Lambdas, Reflections
Einführung in die Programmierung Introduction to Programming Prof. Dr
Programming with ANSI C ++
Object Oriented Programming (OOP) LAB # 8
Mini Language Interpreter Programming Languages (CS 550)
Chapter 19 Generics Dr. Clincy - Lecture.
Inheritance Basics Programming with Inheritance
More inheritance, Abstract Classes and Interfaces
Java Programming Language
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Generics.
CISC124 Assignment 4 on Inheritance due today at 7pm.
Computer Programming with JAVA
Einführung in die Programmierung Introduction to Programming Prof. Dr
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Einführung in die Programmierung Introduction to Programming Prof. Dr
CMSC 202 Generics.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
Einführung in die Programmierung Introduction to Programming Prof. Dr
CMPE212 – Reminders Assignment 2 due next Friday.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Generics, Lambdas and Reflection
Presentation transcript:

Einführung in die Programmierung Introduction to Programming Prof. Dr Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7

News (Reminder) Mock exam next week! Attendance is highly recommended The week after we will discuss the results

Today Inheritance Genericity

Inheritance 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) Note that the subsequent slides are not about “multiple inheritance”. 4

Let's play Lego! BRICK LEGO_BRICK LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED

Class BRICK deferred class BRICK feature width: INTEGER depth: INTEGER height: INTEGER color: COLOR volume: INTEGER deferred end

Class LEGO_BRICK class LEGO_BRICK inherit feature number_of_nubs: INTEGER volume: INTEGER do Result := ... end Inherit all features of class BRICK. New feature, number of nubs Implementation of volume.

Class LEGO_BRICK_SLANTED inherit LEGO_BRICK redefine volume end feature volume: INTEGER do Result := ... The feature volume is going to be redefined (=changed). The feature volume comes from LEGO_BRICK

Class LEGO_BRICK_WITH_HOLE inherit LEGO_BRICK redefine volume end feature volume: INTEGER do Result := ... The feature volume is going to be redefined (=changed). The feature volume comes from LEGO_BRICK

Inheritance Notation Notation: Deferred * Effective + * Redefinition ++ * volume* BRICK + volume+ LEGO_BRICK volume++ volume++ + + LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED

Deferred Deferred Deferred classes can have deferred features. A class with at least one deferred feature must be declared as deferred. A deferred feature does not have an implementation yet. Deferred classes cannot be instantiated and hence cannot contain a create clause. Can we have a deferred class with no deferred features? From a interface point of view all queries should have the right to be deferred. Answer: sure we can if we feel it is useful.

Effective Effective Effective classes do not have deferred features (the “standard case”). Effective routines have an implementation of their feature body.

Precursor If a feature was redefined, but you still wish to call the old one, use the Precursor keyword. volume: INTEGER do Result := Precursor - ... end

Today Inheritance Genericity

Genericity - motivation Assume we want to create a list class capable of storing objects of any type. class LIST -- First attempt feature put: (a_item: ANY) do -- Add item to the list end item: ANY -- Return the first item in the list -- More feature for working with the list We could choose ANY as the item type

Working with this list – first attempt insert_strings (a_list_of_strings: LIST) do a_list_of_strings.put(“foo”) a_list_of_strings.put(12); end print_strings (a_list_of_strings: LIST) local l_printme: STRING across a_list_of_strings as l loop l_printme := l.item io.put_string (l_printme) Here we are inserting an INTEGER Compile error: cannot assign ANY to STRING

Working with this list – the right way insert_strings (a_list_of_strings: LIST) do a_list_of_strings.put(“foo”) a_list_of_strings.put(12); end print_strings (a_list_of_strings: LIST) local l_current_item: ANY across a_list_of_strings as l loop l_current_item := l.item if attached {STRING} l_current_item as itemstring then io.put_string (itemstring) else io.put_string (“The list contains a non-string item!”) Still nobody detects this problem This solution works, but wouldn’t it be nice to detect this mistake at compile time? Correct. This synctactical construct is called ‘object test’.

Genericity Genericity lets you parameterize a class. The parameters are types. A single class text may be reused for many different types.

Type parameterization Genericity Inheritance Abstraction SET_OF_CARS Genericity Type parameterization Type parameterization LIST_OF_ CITIES LIST_OF_CARS LIST_OF_ PERSONS LINKED_LIST_OF_CARS Specialization 20

class LIST [ G ] feature extend (x : G ) ... last : G ... end A generic list Formal generic parameter class LIST [ G ] feature extend (x : G ) ... last : G ... end In the class body, G is a valid type name Query last returns an object of type G To use the class: obtain a generic derivation, e.g. cities : LIST [ CITY ] Actual generic parameter

A generic list with constraints class STORAGE [G inherit LIST [G] feature consume_all do from start until after loop item.consume forth end ] -> RESOURCE constrained generic parameter The feature item is checked for conformance with RESOURCE. We can assume this. The feature item is of type G. We cannot assume consume.

Type-safe containers Using genericity you can provide an implementation of type safe containers. x: ANIMAL animal_list: LINKED_LIST [ANIMAL] a_rock: MINERAL animal_list.put (a_rock) -- Does this rock? Compile error!

Definition: Type We use types to declare entities, as in x : SOME_TYPE With the mechanisms defined so far, a type is one of: A non-generic class e.g. METRO_STATION A generic derivation, i.e. the name of a class followed by a list of types, the actual generic parameters, in brackets (also recursive) e.g. LIST [ARRAY [METRO_STATION ]] LIST [LIST [CITY ]] TABLE [STRING, INTEGER]

So, how many types can I possibly get? Two answers, depending on what we are talking about: Static types Static types are the types that we use while writing Eiffel code to declare types for entities (arguments, locals, return values) Dynamic types Dynamic types on the other hand are created at run-time. Whenever a new object is created, it gets assigned to be of some type.

Static types class EMPLOYEE feature name: STRING birthday: DATE end class DEPARTMENT staff: LIST [EMPLOYEE] bound by the program text: EMPLOYEE STRING DATE DEPARTMENT LIST[G] becomes LIST[EMPLOYEE]

Object creation, static and dynamic types class TEST_DYNAMIC _CREATION feature ref_a: A; ref_b: B -- Suppose B, with creation feature make_b, -- inherits from A, with creation feature make_a do_something do create ref_a.make_a -- Static and dynamic type is A create {B} ref_a.make_b -- Static type is A, dynamic type is B create ref_b.make_b ref_a := ref_b end create {B} ref_a.make_b Can be explained like creating the instance of class B and then assigning ref_a to this instance.

Dynamic types: another example class SET[G] feature powerset: SET[SET[G]] is do create Result -- More computation… end i_th_power (i: INTEGER): SET[ANY] require i >= 0 local n: INTEGER Result := Current from n := 1 until n > i loop Result := Result.powerset n := n + 1 Dynamic types from i_th_power : SET[ANY] SET[SET[ANY]] SET[SET[SET[ANY]]] … From http://www.eiffelroom.com/article/fun_with_generics