Generics and The ArrayList Class

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.
Generic programming in Java
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
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 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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract 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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10: Inheritance and Polymorphism
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Chapters (ArrayList / Generic)
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
GENERICS. Generics  Classes and methods can have a type parameter (actually, more than one).  The type parameter can be any reference (class) type.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Introduction to Generics
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces and Inner Classes
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Generics.
Chapter 11: Inheritance and Polymorphism
Java Generics.
Chapter 20 Generic Classes and Methods
Chapter 3: Using Methods, Classes, and Objects
CMSC 202 Generics.
Generic programming in Java
Comp 249 Programming Methodology
Generics.
Java Inheritance.
CMSC 202 Generics.
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

Generics and The ArrayList Class Chapter 14 Generics and The ArrayList Class University Of Ha’il

Part 2 Generics University Of Ha’il

Parameterized Classes and Generics The class ArrayList is a parameterized class It has a parameter, denoted by Base_Type, that can be replaced by any reference type to obtain a class for ArrayLists with the specified base type Starting with version 5.0, Java allows class definitions with parameters for types These classes that have type parameters are called parameterized class or generics Generic class = parameterized class = class with a parameter for a type

Generics A class definition with a type parameter is stored in a file and compiled just like any other class. Once a parameterized class is compiled, it can be used like any other class. However, the class type plugged in for the type parameter must be specified before it can be used in a program. Doing this is said to instantiate the generic class Sample<String> object = new Sample<String>( ); University Of Ha’il

Example (Generics) public class Sample<T> { private T data ; public void setData(T newData) data = newData ; } T is a parameter for a type public T getData( ) return data ; The class Sample could be used as follows: Sample<String> Obj = new Sample<String>( ) ; Obj.setData(“Hello”) ; System.out.println(Obj.getData( ) ) ; University Of Ha’il

Class Definition with a Type Parameter A class that is defined with a parameter for a type is called a generic class or a parameterized class. The type parameter is included in angular brackets after the class name in the class definition heading. Traditionally, a single uppercase letter is used for a type parameter, but any non-keyword identifier may be used. University Of Ha’il

Example (A Generic Class for Ordered Pairs) public class Pair<T> { private T first; private T second; public Pair() first = null; second = null; } public Pair(T firstItem, T secondItem) first = firstItem; second = secondItem; public void setFirst(T newFirst) first = newFirst; public void setSecond(T newSecond) second = newSecond; public T getFirst() { return first; } public T getSecond() return second; University Of Ha’il

Output: Enter two words: two words You guessed incorrectly. You guessed first: two second: words The secret words are first: Happy second: Day

A Generic Constructor Name Has No Type Parameter Although the class name in a parameterized class definition has a type parameter attached, the type parameter is not used in the heading of the constructor definition: public Pair<T>() // Illegal A constructor can use the type parameter as the type for a parameter of the constructor, but in this case, the angular brackets are not used: public Pair(T first, T second) // legal When a generic class is instantiated, the angular brackets are used: Pair<String> pair = new Pair<STring>("Happy", "Day"); University Of Ha’il

A Primitive Type Cannot be Plugged in for a Type Parameter The type plugged in for a type parameter must always be a reference type. It cannot be a primitive type such as int, double, or char. However, now that Java has automatic boxing, this is not a big restriction. Autoboxing, introduced in Java 5, is the automatic conversion the Java compiler makes between the primitive (basic) types and their corresponding object wrapper classes (eg, int and Integer, double and Double, etc). The underlying code that is generated is the same, but autoboxing provides a sugar coating that avoids the tedious and hard-to-read casting typically required by Java Collections, which can not be used with primitive types. University Of Ha’il

Using Generic Classes and Automatic Boxing

Limitations on Type Parameter Usage Within the definition of a parameterized class definition, there are places where an ordinary class name would be allowed, but a type parameter is not allowed. In particular, the type parameter cannot be used in simple expressions using new to create a new object. For instance, the type parameter cannot be used as a constructor name or like a constructor: T object = new T(); T[] a = new T[10]; University Of Ha’il

An Instantiation of a Generic Class Cannot be an Array Base Type Arrays such as the following are illegal: Pair<String>[] a = new Pair<String>[10]; Although this is a reasonable thing to want to do, it is not allowed given the way that Java implements generic classes. University Of Ha’il

A Class Definition Can Have More Than One Type Parameter A generic class definition can have any number of type parameters. Multiple type parameters are listed in angular brackets just as in the single type parameter case, but are separated by commas. University Of Ha’il

Example (Using a Generic Class With two Type Parameters) public class TowTypePair<T1 , T2> { private T1 first; private T2 second; public TowTypePair () first = null; second = null; } public TowTypePair(T1 firstItem, T2 secondItem) first = firstItem; second = secondItem; public void setFirst(T1 newFirst) first = newFirst; public void setSecond(T2 newSecond) { second = newSecond; } public T1 getFirst() return first; public T2 getSecond() return second; University Of Ha’il

Program Output:

A Generic Class Cannot Be an Exception Class It is not permitted to create a generic class with Exception, Error, Throwable, or any descendent class of Throwable. A generic class cannot be created whose objects are throwable public class GEx<T> extends Exception // Illegal The above example will generate a compiler error message. University Of Ha’il

Generic Interfaces An interface can have one or more type parameters. The details and notation are the same as they are for classes with type parameters. University Of Ha’il

Generic Methods When a generic class is defined, the type parameter can be used in the definitions of the methods for that generic class. In addition, a generic method can be defined that has its own type parameter that is not the type parameter of any class. A generic method can be a member of an ordinary class or a member of a generic class that has some other type parameter. The type parameter of a generic method is local to that method, not to the class. University Of Ha’il

Generic Methods The type parameter must be placed (in angular brackets) after all the modifiers, and before the returned type. public static <T> T genMethod(T[] a) University Of Ha’il

Inheritance With Generic Classes A generic class can be defined as a derived class of an ordinary class or of another generic class. As in ordinary classes, an object of the subclass type would also be of the superclass type. Given two classes: A and B, and given G: a generic class, there is no relationship between G<A> and G<B> This is true regardless of the relationship between class A and B, e.g., if class B is a subclass of class A University Of Ha’il