Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Generics Programming in C# Generics CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Java Review Interface, Casting, Generics, Iterator.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Generic programming in Java
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Java Generics.
1 Generics, Type Safety, and Dynamic Data Structures.
Generic Programming David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
CPS 108 : Spring From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea  Not really a standard language like C++  Arguably.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Data Structures Using C++ 2E
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
1-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Effective Java: Generics Last Updated: Spring 2009.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapters (ArrayList / Generic)
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 18 Java Collections Framework
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Generic Instructor : Sarah Alodan. Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
CMSC 330: Organization of Programming Languages Java Generics.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Motivation for Generic Programming in C++
Sixth Lecture ArrayList Abstract Class and Interface
Generic Programming David Rabinowitz.
C++ Templates.
Chapter 19 Java Data Structures
Generic Programming David Rabinowitz.
Starting Out with C++ Early Objects Eighth Edition
Java Generics.
Generic programming in Java
Generics, Lambdas, Reflections
Java Programming Language
Introduction to Java Collection
Presentation transcript:

Generic Programming David Rabinowitz

June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack receives only Object s The problem: We need different stacks for int, String and Method. We don’t want the problems of: Stack.add(“clearly not a method”); … Method m = (Method)stack.pop();

June 14, 2006 Object Oriented Design Course 3 Solution (?) Let’s create IntStack, StringStack and MethodStack. All of them will rely on the original Stack as internal implementation. Are there any problems? A lot of code duplication It’s hard to add new types

June 14, 2006 Object Oriented Design Course 4 Another problem We want to use a swap function between two int s. void swap(int& a, int&b) { int temp = a; a = b; b = temp; } What about swap(double&, double&) and swap(Method&, Method&) ?

June 14, 2006 Object Oriented Design Course 5 The actual solution Generic programming. Write the code once, and worry about the type at compile time The code is suitable to all types Easy to add types later No code duplication Demands from types

June 14, 2006 Object Oriented Design Course 6 So how do we do it? swap (T& a, T& b) { T temp = a; a = b; b = temp; } Looks simple?

June 14, 2006 Object Oriented Design Course 7 Uses Containers list set vector map Algorithms sort search copy

June 14, 2006 Object Oriented Design Course 8 C++ Templates Most famous use of generic programming STL – Standard Template Library Containers vector, set, hash_map Algorithms for_each, swap, binary_search, min, max Very high performance library Compile time, Inlining, Specializations, …

June 14, 2006 Object Oriented Design Course 9 What about Java? Until 1.5, Java had a large collection set Set, List, Map, Iterator, and more sort(), search(), fill(), copy(), max(), min() The collections were not type safe No problem to do: Map.put(“key”, “4”); Integer i = (Integer)map.get(“key”

June 14, 2006 Object Oriented Design Course 10 Java Generics JSR 14, add in Java 1.5 (“Tiger”) Done in the compiler only Converts: String s = vector.get(3) to: String s = (String)vector.get(3) “Forward Compatible” with earlier JDKs For objects only (not simple types)

June 14, 2006 Object Oriented Design Course 11 Using Java Generics List myIntList = new LinkedList (); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next();

June 14, 2006 Object Oriented Design Course 12 Generic Collections public interface List { void add(E x); Iterator iterator(); } public interface Iterator { E next(); boolean hasNext(); }

June 14, 2006 Object Oriented Design Course 13 Subtyping List ls = new ArrayList (); List lo = ls; lo.add(new Object()); // attempts to assign an Object // to a String! String s = ls.get(0);

June 14, 2006 Object Oriented Design Course 14 Subtyping (cont.) Foo is subtype of Bar Foo extends Bar Foo implements Bar C is a generic container C Results that C is not subtype of C

June 14, 2006 Object Oriented Design Course 15 Generic Algorithms (1) How to print entire Collection? Do we have to use Collection ? Use wildcard void printCollection(Collection c){ for (Object e : c) { System.out.println(e); }

June 14, 2006 Object Oriented Design Course 16 Generic Algorithms (2) What happens when we want to use a specific method? public void drawAll(List shapes) { for (Shape s: shapes) { s.draw(this); } What about subtyping? List

June 14, 2006 Object Oriented Design Course 17 Generic Algorithms (3) The solution public void drawAll(List shapes) {…}

June 14, 2006 Object Oriented Design Course 18 More About Wildcards Collection c = new ArrayList (); c.add(new Object()); public void addRectangle(List shapes) { shapes.add(0, new Rectangle()); }

June 14, 2006 Object Oriented Design Course 19 C# Generics Were added to.NET 2.0 Generics designed in advance Visible to reflection, bytecode manipulation Very similar to Java: public struct Point { public T X; public T Y; } Point point; point.X = 1; point.Y = 2; More at:

June 14, 2006 Object Oriented Design Course 20 C++ versus Java/C# Implementation The C++ Approach to wildcards Use any required method/operator The compiler verifies that all methods exist For example, STL assumes default constructor More flexible, yet undocumented Weird compiler errors Templates only compiled when instantiated

June 14, 2006 Object Oriented Design Course 21 C++ Template Instantiation Multiple executable code segments may be compiled from one template Stack, Stack, Stack This doesn’t happen in C#/Java Specialization may define optimizations template class Vector {…} template<> Vector { …} template class Vector : private Vector { …}

June 14, 2006 Object Oriented Design Course 22 Summary Generics are a required feature Containers, Reusable algorithms Different Approaches C++ - Performance first, non-OO Java – Safety first, Forward Compatible C# - Safety first, integrate with OOP