1 Generics Fall 2005 OOPD John Anthony. 2John J. Anthony Object Oriented Programming & Design Remember the Container Problem? ? You know the color of.

Slides:



Advertisements
Similar presentations
Comp 212: Intermediate Programming Lecture 18 – Java Generics By: Anupam Chanda.
Advertisements

1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
Generic programming in Java
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Generics.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Introduction to Inheritance Fall 2005 OOPD John Anthony.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.
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.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Java Generics.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
Effective Java: Generics Last Updated: Spring 2009.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Computer Science 209 Software Development Java Collections.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Generics. What is Generics Collections can store Objects of any Type Generics restricts the Objects to be put in a collection Generics ease identification.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Generics CompSci 230 S Software Construction.
Types in programming languages1 What are types, and why do we need them?
Introduction to Generics
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
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.
Polymorphism (generics) CSE 331 University of Washington.
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.
©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.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Generics Starring: Rite-Aid Aspirin Co-Starring: CVS Paper Plates.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
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.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Inheritance and Polymorphism
Inheritance and Encapsulation
More on Java Generics Multiple Generic Types Bounded Generic Types
Software Engineering Fall 2005
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Interfaces and Inheritance
עקרונות תכנות מונחה עצמים תרגול 21: Generics
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Generic programming in Java
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Chapter 9 Carrano Chapter 10 Small Java
Arrays and ArrayLists.
Presentation transcript:

1 Generics Fall 2005 OOPD John Anthony

2John J. Anthony Object Oriented Programming & Design Remember the Container Problem? ? You know the color of the balls as they are placed in the container… …but what color are they when they come out?

3John J. Anthony Object Oriented Programming & Design For Example… protected void inspectCollection(Collection aCollection) { Iterator i = aCollection.iterator(); while (i.hasNext()) { String element = (String) i.next(); } protected void collectionsExample() { ArrayList list = new ArrayList(); list.add(new String("test string")); list.add(new Integer(9)); inspectCollection(list); }

4John J. Anthony Object Oriented Programming & Design The Problem We desire a mechanism by which containers can be restricted to specific types. Solution 1: Build a mechanism into the language that allows the developer to specify the type at compile time. Solution 2: Create new classes for each type we desire (e.g. ArrayOfStrings, ArrayOfIntegers, etc.)

5John J. Anthony Object Oriented Programming & Design Enter Generics protected void collectionsExample() { ArrayList list = new ArrayList (); list.add(new String("test string")); // list.add(new Integer(9)); this no longer compiles inspectCollection(list); } protected void inspectCollection(Collection aCollection) { Iterator i = aCollection.iterator(); while(i.hasNext()) { String element = i.next(); } Parameterized Type A class that uses type variables to declare its type at compile time.

6John J. Anthony Object Oriented Programming & Design Parameterized Type public class ArrayList extends AbstractList { // details omitted... public void add(E element) { // details omitted } public Iterator iterator() { // details omitted } } //end class Type Variable An unqualified identifier that serves as a placeholder for type identification.

7John J. Anthony Object Oriented Programming & Design Some Benefits of Generics Type Safety Type Safety The Collection classes are now type safe without type variation. The Collection classes are now type safe without type variation. Stronger ‘Contract’ Stronger ‘Contract’ Generics supports stronger interfaces between the client and supplier through type declarations. Generics supports stronger interfaces between the client and supplier through type declarations. Early Binding Early Binding Typing errors can be caught at compile time rather than runtime. Typing errors can be caught at compile time rather than runtime.

8John J. Anthony Object Oriented Programming & Design Bounded Genericity You are creating a binary tree data structure (that can be persisted to disk) and you choose to define your class as a parameterized type. public class BinaryTree { // details omitted... public void insert(E element) {…} } //end class How can you guarantee that the type variable ‘E’ will be Comparable and Serializable?

9John J. Anthony Object Oriented Programming & Design Solution public class BinaryTree { // details omitted... public void insert(E element) {…} } //end class Garanteed that all elements of type ‘E’ will contain the “compareTo(…) method and implement the Serializable interface

10John J. Anthony Object Oriented Programming & Design Generic Methods public T max(T t1, T t2) { if (t1.compareTo(t2) > 0) return t1; else return t2; } Client Code Supplier Code Ex 1: Integer iresult = max(new Integer(100), new Integer(200)); Ex 2: String sresult = max("AA", "BB"); Ex 3: Number nresult = max(new Integer(100), "AAA"); Returns a value whose type is the nearest shared type of the two parameters. See any problems?

11John J. Anthony Object Oriented Programming & Design Example of Nearest Type Being Returned Example.java:10: incompatible types found : java.lang.Object&java.io.Serializable&java.lang.Comparable required: java.lang.Number Number nresult = max(new Integer(100), "AAA"); Compiler Error

12John J. Anthony Object Oriented Programming & Design Inheritance & Overloading Classes can extend generic classes, and provide values for type parameters or add new type parameters by in doing so. For example, all the following are legal: class MyStringList extends ArrayList {... } class A {... } class B extends A {... }

13John J. Anthony Object Oriented Programming & Design Inheritance & Overloading Generics changes the rules of method overriding slightly. ArrayList: public Object get(int i); MyStringList: public String get(int i);