CIS 270—Application Development II Chapter 18-Generics.

Slides:



Advertisements
Similar presentations
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Advertisements

Computer Science 209 Software Development Equality and Comparisons.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Generic programming in Java
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
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.
 2006 Pearson Education, Inc. All rights reserved Generics.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
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.
Chapter 3 Introduction to Collections – Stacks Modified
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.
 2005 Pearson Education, Inc. All rights reserved Generics.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
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.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Peyman Dodangeh Sharif University of Technology Spring 2014.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
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.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
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 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
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.
 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.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Chapter VII: Arrays.
Programming with ANSI C ++
Chapter 20 Generic Classes and Methods
Chapter 14 Templates C++ How to Program, 8/e
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS2011 Introduction to Programming I Methods (II)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Generic Programming.
Generics 5/11/2019.
Chapter 19 Generics.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Generic Classes and Methods
Corresponds with Chapter 5
Presentation transcript:

CIS 270—Application Development II Chapter 18-Generics

Introduction Generic ________ allow the programmer to specify, in a single method declaration, a set of related methods. e.g., one generic method could sort arrays of different types Generic ________ allow the programmer to specify, in a single class declaration, a set of related types. e.g., one generic class could be used to instantiate array objects for an array of Integers, Doubles, Strings, etc. Compile-time type _______ could be enforced so that an array stores elements of the same type. Generics only work with ___________ data types.

Motivation for Generic Methods Separate methods, all called printArray, can be defined to print the elements of any type of array, such as Integer[], Double[], or Character[]. public static void printArray( Integer[] inputArray ) { for ( Integer element : inputArray ) System.out.printf( "%s ", element ); System.out.println(); } // see Fig for complete program It would be convenient to define one printArray method for any array of generic type E[] and have the Java compiler determine the data types of different arrays used in an application.

Generic Methods: Implementation Following is an implementation of a generic method: public static void printArray( E[] inputArray ) { for ( E element : inputArray ) System.out.printf( "%s ", element ); System.out.println(); } The E is an arbitrary type ___________ that serves as a placeholder for an actual argument. The compiler checks the program to set up method calls to printArray for different data types. The compiler also checks for valid operations.

Type Parameters as Return Types See Fig public static > T maximum( T x, T y, T z ) // first T is return type { T max = x; // assume x is the largest if ( y.compareTo( max ) > 0 ) max = y; // y is the largest so far if ( z.compareTo( max ) > 0 ) max = z; // z is the largest return max; // returns the largest object } // end method maximum If primitives are sent to maximum, the compiler _________ them as objects (and compareTo is valid).

Overloading Generic Methods A class can have two different generic methods with the same name, but different method parameters. A generic method can be overloaded by non-generic methods of the same name and number of parameters. The compiler first looks for an exact match of method names and argument types. If an exact match isn’t found, the compiler looks for __________. _________ is the process whereby the compiler replaces type parameters with actual types. Type parameters can have an upper _________.

Generic Classes A generic class is used to describe something in a type-independent manner. You can specify the actual types that should be used in place of the class’s ______ parameter(s). For example, one generic Stack class could be used to create many Stack classes. A stack is a data structure (list of elements) with a ______ (last in, first out) organization. An element is pushed onto the stack and popped off of the stack (like dishes in a stack of dishes). Fig presents a generic Stack class declaration that uses an array of type E to implement the Stack.

Raw Types If a class is instantiated without specifying the type argument, Java will use type Object. Stack objectStack = new Stack( 5 ); instead of Stack doubleStack = new Stack ( 5 ); This ensures ___________ compatibility with earlier versions of Java. A raw type Stack variable can be assigned a generic stack: Stack rawTypeStack = new Stack ( 5 ); A generic type Stack variable can be assigned a raw type, but it is ____________: Stack integerStack = new Stack( 10 );