Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Generic programming in Java
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
Java Generics.
Generic Programming David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
J2SE 5.0 New Features. J2SE 5.0 aka JDK 1.5 aka Tiger.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want.
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java Generics.
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.
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.
Effective Java: Generics Last Updated: Spring 2009.
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.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
Generics CompSci 230 S Software Construction.
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
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
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
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1.
©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.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
FINAL REVIEW. Stronger vs Weaker (one more time!) Requires more? Promises more? (stricter specifications on what the effects entail) weaker stronger.
1 OPI Lecture 22 Java generic types & classes Java collection library Kasper Østerbye Carsten Schuermann IT University Copenhagen.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Sixth Lecture ArrayList Abstract Class and Interface
Generic Programming David Rabinowitz.
Chapter 19 Java Data Structures
CIS265/506 Cleveland State University – Prof. Victor Matos
Introducing Java Generics and Collections
Chapter 20 Generic Classes and Methods
Java Generics Lecture 14 CS2110 – Fall 2016
Generic Programming David Rabinowitz.
Java Generics.
Chapter 19 Generics Dr. Clincy - Lecture.
Generic programming in Java
Chapter 19 Generics.
Chapter 21 Generics.
Generics, Lambdas, Reflections
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Chapter 21 Generics.
Chapter 19 Generics.
Chapter 19 Generics.
Presentation transcript:

Generic Programming Amit Shabtay

March 3rd, 2004 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();

March 3rd, 2004 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

March 3rd, 2004 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&) ?

March 3rd, 2004 Object Oriented Design Course 5 The Actual Solution Generic programming. (The ability to have type parameters on your type) 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

March 3rd, 2004 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?

March 3rd, 2004 Object Oriented Design Course 7 Java vs. 1.5 and Autoboxing ArrayList list = new ArrayList(); //1.4.2 list.add(0, new Integer(42)); int total = ((Integer)list.get(0)).intValue(); ArrayList list = new ArrayList ();//1.5 list.add(0, new Integer(42)); int total = list.get(0).intValue(); ArrayList list = new ArrayList (); //1.5 auto-boxing list.add(0, 42); int total = list.get(0);

March 3rd, 2004 Object Oriented Design Course 8 Uses Containers list set vector map Algorithms sort search copy

March 3rd, 2004 Object Oriented Design Course 9 C++ Templates The most known use of generic programming STL – Standard Template Library Containers vector, set, hash_map Algorithms for_each, swap, binary_search, min, max

March 3rd, 2004 Object Oriented Design Course 10 What about Java? Until now Java had large collection set Set, List, Map, Iterator, and more sort(), search(), fill(), copy(), max(), min() One major problem – the collections are not type safe No problem to do Map.put(“key”, “4”); Integer i = (Integer)map.get(“key”);

March 3rd, 2004 Object Oriented Design Course 11 Java Generics Added as one of the new features of Java 1.5 (“Tiger”) Done in the compiler only Converts String s = vector.get(3) to String s = (String)vector.get(3)

March 3rd, 2004 Object Oriented Design Course 12 How to Use Generics ? List myIntList = new LinkedList (); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next();

March 3rd, 2004 Object Oriented Design Course 13 And What About the Collection ? public interface List { void add(E x); Iterator iterator(); } public interface Iterator { E next(); boolean hasNext(); }

March 3rd, 2004 Object Oriented Design Course 14 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); //Compiler will not permit

March 3rd, 2004 Object Oriented Design Course 15 Subtyping (cont.) Foo is subtype of Bar if: Foo extends Bar Foo implements Bar C is a generic container C Results that C is not subtype of C

March 3rd, 2004 Object Oriented Design Course 16 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); }

March 3rd, 2004 Object Oriented Design Course 17 Generic Algorithms (2) What happens when we want to use specific method? public void drawAll(List shapes) { for (Shape s: shapes) { s.draw(this); } What about subtyping? List

March 3rd, 2004 Object Oriented Design Course 18 Generic Algorithms (3) The solution public void drawAll(List shapes) {…} //Called bounded wildcard.

March 3rd, 2004 Object Oriented Design Course 19 More About Wildcards Collection c = new ArrayList (); c.add(new Object()); public void addRectangle(List shapes) { shapes.add(0, new Rectangle()); }

March 3rd, 2004 Object Oriented Design Course 20 Super vs. Extends The syntax ? super T denotes an unknown type that is a supertype of T. It is the dual of the ? extends T to denote an unknown type that is a subtype of T.

March 3rd, 2004 Object Oriented Design Course 21 Collection vs Collection vs Collection Collection is a collection of heterogeneous instances of potentially no common type Collection is a collection of homogeneous instances of some common types – we just don’t know what that common type is Collection is a raw type – we should avoid it

March 3rd, 2004 Object Oriented Design Course 22 Many More Features Java Generics are one of the important language features of Java 1.5 More information in alArticles/J2SE/generics/ alArticles/J2SE/generics/ J2SE 5.0 in a Nutshell alArticles/releases/j2se15/ alArticles/releases/j2se15/

March 3rd, 2004 Object Oriented Design Course 23 Java Generics Summary Java Generics use a technique known as type erasure which is the process of translating or rewriting code that uses generics into non-generic code all information between angle brackets is erased.

March 3rd, 2004 Object Oriented Design Course 24 C# Generics Very similar to Java public struct Point { public T X; public T Y; } Point point; point.X = 1; point.Y = 2; See for more informationhttp://msdn.microsoft.com/

March 3rd, 2004 Object Oriented Design Course 25 Java Generics vs. C++ Templates vs. C# Java borrowed C++ syntax and made it mean something very different Type erasure vs. code generation in C++. Evaluated in compile time in java and c++, run time in c#