Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.

Slides:



Advertisements
Similar presentations
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Advertisements

Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Generics, Lists, Interfaces
Java Review Interface, Casting, Generics, Iterator.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Generic programming in Java
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
Object-Oriented PHP (1)
Polymorphism, Virtual Methods and Abstract Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
JAVA Introduction ● One of the main JAVA design goal is reducing complexity for programmer – Development time is half or less comparing to equivalent C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
1-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Chapter 12 Support for Object oriented Programming.
Object Oriented Software Development
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Object-Oriented Programming Chapter Chapter
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
More About Classes Ranga Rodrigo. Quiz: Question 1 Write the following C++ code in Eiffel. void covert { int i ; float f(1.41) ; i = (int) f ; }
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism, Virtual Methods and Abstract Classes
Types of Programming Languages
Designing for Inheritance
Chapter 19 Generics Dr. Clincy - Lecture.
Generic programming in Java
Generics.
Fundaments of Game Design
Interfaces.
Java Programming Language
Review: libraries and packages
Presentation transcript:

Genericity Ranga Rodrigo Based on Mark Priestley's Lectures

What is Genericity? Generic components in programming languages are a means of providing reusability of source code. The classic use of generics is to implement resuable data structures, such as lists, trees and so on.

What is Genericity? Such classes need to know very little about the data that is being stored in them: the source code for a list of integers will be almost identical to that for a list of strings, for example. What is required is a secure way to write, for example, a list class which can be used to store data of different types at different times, without changing the source code.

Genericity in Untyped Languages This happens automatically in an untyped language, such as Perl. Untyped languages place no restriction on what can be stored in a data structure. Consequences of this are: Code reuse is enabled: a single list can store data integers on one occasion and strings on another. Data structures can be heterogeneous: a single structure can store data of a mixture of types. It is the programmer's responsibility to remember what data is in the list: this is a common source of programming errors.

Using inheritance Inheritance is a mechanism for code reuse, so it is natural to wonder if it can be used for generic data structures. This is done by identifying a root class in the inheritance hierarchy, and storing references to this class in the data structure. By inheritance, the data structure can then hold data of any sort.

Using inheritance For example, some of the Java classes follow this approach, such as the java.util.Vector class. public class Vector { protected Object elementData[] ; public Object firstElement() {... } public void addElement(Object obj) {... } }

Using inheritance This data structure stores an array of references to Object, and the methods for inserting and accessing data items in the vector are defined in terms of Object. Because a reference to any Java class can be converted to a reference to Object, this means that any Java object can be stored in a vector, and hence that the vector class can be reused to store different data types.

Using inheritance Vector personVector, moduleVector ; Person p = new Person("John") ; Module m = new Module("3SFE613") ; personVector.addElement(p) ; moduleVector.addElement(m) ;

Insecurity Due to Using Inheritance However, in some ways this reintroduces an untyped element into an otherwise typed language. When you retrieve an object from a vector, you have to perform an (insecure) cast to get it back to its original type: Person p = (Person) personVector.firstElement();

Using Inheritance Also, this approach allows heterogeneous data structures: Finally, the vector class cannot be used for data of Java's basic types, as they are not class- based and so do not inherit from Object. moduleVector.addElement(p) ;

Parameterized Classes A better solution to this problem is to provide classes with type parameters: These are known as generic classes in Eiffel. C++ provides a similar facility with templates. class VECTOR[G] feature addElement(g : G)... firstElement : G... end

Generic Classes Because the type is a parameter, an actual type must be provide before the class can be used: this process is called derivation or instantiation: Note that one generic class can give rise to many types, one for each possible value of the generic parameter. people : VECTOR[PERSON] modules : VECTOR[MODULE]

Generic Classes: Compiler Detects The compiler can now detect attempts to store the wrong sort of data in a vector, and type errors when data is removed from the list can be detected: Also, data of any type can be stored, so `basic types' can be used in data structures as well as class types (this applies to mixed languages like C++ as well as to pure OO languages like Eiffel). people.addElement(m) -- type error m := modules.firstElement -- type error

Using Generic Entities Inside the definition of a generic class, you know virtually nothing about the generic parameter, so the class is limited to basic operations such as assignment, testing for equality and so on. In many data structures, the data is purely passive, so this is all that is required. We might have a requirement to add together two vectors, however, by adding their matching components. You could try to implement this as follows:

class VECTOR[G] inherit ARRAY[G] feature add(v : VECTOR[G]) : VECTOR[G] is do from i := lower until i > upper loop Result.put( item(i) + v.item(i), i) end

However, the compiler has no way of telling that the elements in the vector support the + operation, so this will not compile. What is required is a way of specifying that any class substituted for the generic parameter must have certain properties. In Eiffel, this is done by defining the required properties in a deferred class, rather like an interface definition in Java. In this case, we require the addition operator so we might define:

deferred class ADDABLE feature infix "+"(x : ADDABLE) : ADDABLE is deferred end Now what's required is a way of specifying, in the VECTOR class that only classes which are descendants of ADDABLE are acceptable instantiations of the generic parameter. This is done by using constrained genericity, as follows:

class VECTOR[G -> ADDABLE] inherit ARRAY[G] etc

Genericity and Inheritance As the examples above have illustrated, genericity and inheritance are orthogonal mechanisms in Eiffel, meaning that the use of one does not affect the possible uses of the other. In particular, a generic class is a perfectly normal class, and can be specified as a parent of another class. Finally, polymorphic data structures can be achieved in Eiffel by providing a generic parameter of a suitable parent class:

bank : VECTOR[ACCOUNT] The bank object will be able to store accounts of any class derived from ACCOUNT. Extracting items from such a list runs into the same issue as with Java data structures, namely that you don't know what class an extracted object belongs to. Eiffel doesn't permit casting: instead you can use a reverse assignment attempt: If the first item in the bank vector is not a savings account, s will be Void after this attempt. s : SAVING_ACCOUNT s ?= bank.item(1)

If the first item in the bank vector is not a savings account, s will be Void after this attempt.