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:



Advertisements
Similar presentations
Computer Science 209 Software Development Equality and Comparisons.
Advertisements

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Generic programming in Java
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
 2006 Pearson Education, Inc. All rights reserved Generics Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides.
 2006 Pearson Education, Inc. All rights reserved Templates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
 2006 Pearson Education, Inc. All rights reserved Generics.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Templates Zhen Jiang West Chester University
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Java Generics.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
 2005 Pearson Education, Inc. All rights reserved Generics.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CIS 270—Application Development II Chapter 18-Generics.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
CSI 3125, Preliminaries, page 1 Generic Class & Generic methods.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
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:
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
 2006 Pearson Education, Inc. All rights reserved Templates.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
 Pearson Education, Inc. All rights reserved. 1 Ch 18 Generics OBJECTIVES In this chapter you will learn:  To create generic methods that perform.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C++ Systems Programming.
Chapter 20 Generic Classes and Methods
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
Chapter 5 Function Basics
CS2011 Introduction to Programming I Methods (II)
Generic Programming.
Chapter 19 Generics.
Generic Classes and Methods
Presentation transcript:

1 L39 Generics (1)

2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack class that can be used to store objects of any class or interface type.  To understand how to overload generic methods with non-generic methods or with other generic methods.

Introduction Generics – New feature of J2SE 5.0 – Provide compile-time type safety Catch invalid types at compile time – Generic methods A single method declaration A set of related methods – Generic classes A single class declaration A set of related clases

Motivation for Generic Methods Overloaded methods – Perform similar operations on different types of data – Overloaded printArray methods Integer array Double array Character array – Only reference types can be used with generic methods and classes

5 Outline OverloadedMethods.java (1 of 3) Line 7 Line 17 Method printArray accepts an array of Integer objects Method printArray accepts an array of Double objects

6 Outline OverloadedMethods.java (2 of 3) Line 27 Method printArray accepts an array of Character objects

7 Outline OverloadedMethods.java (3 of 3) Line 44 Line 46 Line 48 Program output At compile time, the compiler determines argument integerArray ’s type (i.e., Integer []), attempts to locate a method named printArray that specifies a single Integer [] parameter (lines 7-14) At compile time, the compiler determines argument doubleArray ’s type (i.e., Double []), attempts to locate a method named printArray that specifies a single Double [] parameter (lines 17-24) At compile time, the compiler determines argument characterArray ’s type (i.e., Character []), attempts to locate a method named printArray that specifies a single Character [] parameter (lines 7-14)

Motivation for Generic Methods (Cont.) Study each printArray method – Array element type appears in two location Method header for statement header Combine three printArray methods into one – Replace the element types with a generic name E – Declare one printArray method Display the string representation of the elements of any array

9 Outline Fig | printArray method in which actual type names are replaced by convention with the generic name E. Replace the element type with a single generic type E

Generic Methods: Implementation and Compile-Time Translation Reimplement Fig using a generic method – Method calls are identical – Outputs are identical Generic method declaration – Type parameter section Delimited by angle brackets ( ) Precede the method’s return type Contain one or more type parameters – Also called formal type paramters

Generic Methods: Implementation and Compile-Time Translation Type parameter – Also known as type variable – An identifier that specifies a generic type name – Used to declare return type, parameter types and local variable types – Act as placeholders for the types of the argument passed to the generic method Actual type arguments – Can be declared only once but can appear more than once public static void printTwoArrays( E[] array1, E[] array2 )

12 Outline GenericMethodTest.java (1 of 2) Line 7 Lines 10 Type parameter section delimited by angle brackets ( ) Use the type parameter to declare method printArray ’s parameter type Use the type parameter to declare method printArray ’s local variable type

13 Outline GenericMethodTest.java (2 of 2) Line 24 Line 26 Line 28 Program output Invoke generic method printArray with an Integer array Invoke generic method printArray with a Double array Invoke generic method printArray with a Character array

Generic Methods: Implementation and Compile-Time Translation (Cont.) Compile-time translation – Erasure Remove type parameter section Replace type parameters with actual types Default type is Object

15 Outline Fig | Generic method printArray after erasure is performed by the compiler. Remove type parameter section and replace type parameter with actual type Object Replace type parameter with actual type Object

Additional Compile-Time Translation Issues: Methods That Use a Type Parameter as the Return Type Application of Fig – Generic method – Use Type parameters in the return type and parameter list Generic interface – Specify, with a single interface declaration, a set of related types – E.g., Comparable Method integer1.compareTo( integer2 ) – Compare two objects of the same class – Return 0 if two objects are equal – Return -1 if integer1 is less than integer2 – Return 1 if integer1 is greater than integer2

17 Outline MaximumTest.java (1 of 2) Line 7 Line 9 Line Lines Type parameter section specifies that only object of classes that implement interface Comparable can be used with this method Type parameter is used in the return type of method maximum Invokes method compareTo method Comparable to compare y and max Invokes method compareTo method Comparable to compare z and max Assign x to local variable max

18 Outline MaximumTest.java (2 of 2) Line 23 Line 25 Line 27 Program output Invoke generic method maximum with three integers Invoke generic method maximum with three doubles Invoke generic method maximum with three strings

Additional Compile-Time Translation Issues: Methods That Use a Type Parameter as the Return Type (Cont.) Upper bound of type parameter – Default is Object – Always use keyword extends E.g., T extends Comparable – When compiler translates generic method to Java bytecode Replaces type parameter with its upper bound Insert explicit cast operation e.g., line 23 of Fig I preceded by an Integer cast (Integer) maximum( 3, 4, 5 )

20 Outline Erasure replaces type parameter T with its upper bound Comparable

Overloading Generic Method Generic method may be overloaded – By another generic method Same method name but different method parameters – By non-generic methods Same method name and number of parameters When compiler encounters a method call – Search for most precise matching method first Exact method name and argument types – Then search for inexact but applicable matching method