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.

Slides:



Advertisements
Similar presentations
Generics and the ArrayList Class
Advertisements

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Generics and The ArrayList Class
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Abstract Classes and Interfaces
Inheritance and Polymorphism CS351 – Programming Paradigms.
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
Intro to Generic Programming Templates and Vectors.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
Java Generics.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
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.
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)
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Programming in Java CSCI-2220 Object Oriented Programming.
Introduction to 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
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 in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
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:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Chapter 22 Generic Programming. Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Factory Patterns. DCS – SWC 2 Being less concrete One important OO principle is: ”Program to an interface, not an implementation” Interfaces reduces the.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Interfaces & Sub-types Weiss sec Scenario Instructor says: “Implement a class IntegerMath with two methods pow and fact with the following signatures:
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Generics, Exceptions and Undo Command
Polymorphism and access control
Generic programming in Java
Generics.
Unit 3 Test: Friday.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
CMSC 202 Generics.
Chapter 9 Carrano Chapter 10 Small Java
Programming For Big Data
Presentation transcript:

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 to store elements of a certain type in a container, say an ArrayList This ability does not pose any special requirements on the type itself

DCS – SWC 3 Generics public class ArrayList { public ArrayList() {…} public void add(Object e) {…} }

DCS – SWC 4 Generics Since all classes inherit from Object, this will work – in principle However, there is nothing to prevent us from storing elements of different types in the same ArrayList Will complicate processing of the elements Risk for errors at run-time

DCS – SWC 5 Generics A better (safer) solution is to use Generics In a generic class, one or more type parameters are included in the definition Example is the well- known ArrayList

DCS – SWC 6 Generics public class ArrayList { public ArrayList() {…} public void add(E e) {…} } E is the type parameter

DCS – SWC 7 Generics When we wish to use the class, we must instantiate it with a concrete type: –ArrayList We can use the ArrayList for all types, BUT we cannot put two objects of different types into the same ArrayList

DCS – SWC 8 Creating a generic class Creating a generic class is almost like creating an ordinary class However, one or more of the types are turned into type parameters Consider a class for storing a pair of objects, which may have different types Pair

DCS – SWC 9 Creating a generic class public class Pair { private T first; private S second; public Pair(T first, S second) { this.first = first; this.second = second; } public T getFirst() {return first;} public S getSecond() {return second;} }

DCS – SWC 10 Creating a generic method An ordinary method inside a class usually does not need a type parameter Type parameter for class is used However, this is not the case for static methods: public static void print(E[] a)

DCS – SWC 11 Creating a generic method When invoking a generic method, we do not need to specify a type parameter: Car[] carList; ArrayUtil.print(carList); The compiler can figure out what the concrete type of the parameter is

DCS – SWC 12 Type constraints Sometimes, we will only allow types that fulfill some properties Consider the min method: public static E min(E a, E b) { if (a < b) return a; else return b; }

DCS – SWC 13 Type constraints The previous code will only work for types that can be compared Type must implement the Comparable interface We must constrain the type parameter to fulfill this property

DCS – SWC 14 Type constraints public static E min(E a, E b) { if (a < b) return a; else return b; } See Advanced Topic 17.1 for more advanced issues on type constraints

DCS – SWC 15 Under the hood How does the Java Virtual Machine handle generics…? It substitutes the type variables with a concrete type, depending on the type constraints

DCS – SWC 16 Under the hood public class Pair { private T first; private S second; public Pair(T first, S second) {...} public T getFirst() {return first;} public S getSecond() {return second;} }

DCS – SWC 17 Under the hood public class Pair { private Object first; private Object second; public Pair(Object first, Object second) {...} public Object getFirst() {return first;} public Object getSecond() {return second;} }

DCS – SWC 18 Under the hood But will that always work…? Yes, because the compiler has checked that all generic types are used correctly It does however impose some limitations on the code in generic methods: E e = new E(); This is illegal…(why)?

DCS – SWC 19 Under the hood Why does it work like that? Enables pre-generics code to work together with code using generics Does it matter in practice? Not much, but it explains why some perhaps not-so-obvious limitations exist