Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.

Slides:



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

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Generics and the ArrayList Class
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
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
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,
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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.
©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.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
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
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Classes, Interfaces and Packages
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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.
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.
 Pearson Education, Inc. All rights reserved. 1 Ch 18 Generics OBJECTIVES In this chapter you will learn:  To create generic methods that perform.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Generics.
Inheritance and Polymorphism
Java Generics.
Chapter 20 Generic Classes and Methods
Chapter 19 Generics Dr. Clincy - Lecture.
CMSC 202 Generics.
Java Programming Language
Comp 249 Programming Methodology
Java Inheritance.
CMSC 202 Generics.
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

Java Generics

Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations of generic programming in Java To understand the relationship between generic types and inheritance

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 generic definitions, or, simply, 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 object = new Sample (); Generics (Cont’d)

A 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.  Any non-keyword identifier can be used for the type parameter, but by convention, the parameter starts with an uppercase letter.  The type parameter can be used like other types used in the definition of a class. A Class Definition with a Type Parameter (Cont’d)

Tip: Compile with the -Xlint Option There are many pitfalls that can be encountered when using type parameters Compiling with the -Xlint option will provide more informative diagnostics of any problems or potential problems in the code javac –Xlint Sample.java Question: How would you do that in JCreator?

Generic Class Definition: An Example

Generic Class Definition: An Example (Cont’d)

Generic Class Usage: An Example

Generic Class Usage: An Example (Cont’d) Program Output:

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 () 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) However, when a generic class is instantiated, the angular brackets are used: Pair pair = new Pair ("Happy", "Day");

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.  Note: Reference types can include arrays.

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];

Limitations on Generic Class Instantiation Arrays such as the following are illegal: Pair [] a = new Pair [10]; Although this is a reasonable thing to want to do, it is not allowed given the way that Java implements generic classes

Using Generic Classes and Automatic Boxing

Using Generic Classes and Automatic Boxing (Cont’d) Program Output:

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. Multiple Type Parameters

Multiple Type Parameters (Cont’d)

A Generic Classes and Exceptions 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 extends Exception  The above example will generate a compiler error message

Using a Generic Class with Two Type Parameters Program Output:

Bounds for Type Parameters Sometimes it makes sense to restrict the possible types that can be plugged in for a type parameter T.  For instance, to ensure that only classes that implement the Comparable interface are plugged in for T, define a class as follows: public class RClass  " extends Comparable " serves as a bound on the type parameter T.  Any attempt to plug in a type for T which does not implement the Comparable interface will result in a compiler error message.

A bound on a type may be a class name (rather than an interface name)  Then only descendent classes of the bounding class may be plugged in for the type parameters: public class ExClass A bounds expression may contain multiple interfaces and up to one class. If there is more than one type parameter, the syntax is as follows: public class Two Bounds for Type Parameters (Cont’d)

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.

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.

The type parameter must be placed (in angular brackets) after all the modifiers, and before the returned type: public static T genMethod(T[] a) When one of these generic methods is invoked, the method name is prefaced with the type to be plugged in, enclosed in angular brackets String s = NonG. genMethod(c); Generic Methods (Cont’d)

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 and G  This is true regardless of the relationship between class A and B, e.g., if class B is a subclass of class A

A Derived Generic Class: An Example

A Derived Generic Class: An Example (Cont’d) Program Output: