Generics method and class definitions which involve type parameters.

Similar documents
Java Generics -- an introduction. Based on

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Generics and the. ArrayList Class CHAPTER

Collections Algorithms

Introduction to Programming Using Java (98-388)

Polymorphism. return a.doublevalue() + b.doublevalue();

Generalized Code. Fall 2011 (Honors) 2

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

generic programming alberto ferrari university of parma

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Object-Oriented Programming in the Java language

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Introduction to Generics. Defining a Class with a Type Parameter

Practice for Chapter 11

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

COE318 Lecture Notes Week 8 (Oct 24, 2011)

CMSC 341. Nilanjan Banerjee

COE318 Lecture Notes Week 9 (Oct 31, 2011)

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

CS108, Stanford Handout #8. Java Generics

CLASS DESIGN. Objectives MODULE 4

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Multiple Inheritance, Abstract Classes, Interfaces

Rules and syntax for inheritance. The boring stuff

CS 310: HW 1, Junit, Generics Review

Java Programming. Abstract classes and Interfaces

CO Java SE 8: Fundamentals

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Advanced Programming Generics Collections

Class, Variable, Constructor, Object, Method Questions

Generics, I/O & Regular Expressions Maria Zontak

Chapter 4 Defining Classes I

Exercise 8 Parametric polymorphism November 18, 2016

Java Programming Training for Experienced Programmers (5 Days)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

More On inheritance. What you can do in subclass regarding methods:

Java 5 New Language Features

Introduction to Inheritance

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Problems. Java Generics. Example. Example. Often you need the same behavior for different kind of classes

Index COPYRIGHTED MATERIAL

The class Object. Lecture CS1122 Summer 2008

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Java Generics

Advanced Topics in Java and on Security in Systems Effective Generics. Eirik Eltvik 26 th of February 2007

Comp 249 Programming Methodology

What is Inheritance?

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

Java Object Oriented Design. CSC207 Fall 2014

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

COP 3330 Final Exam Review

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Exam Duration: 2hrs and 30min Software Design

Java: exceptions and genericity

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Principles of Computer Science

CS-202 Introduction to Object Oriented Programming

Declarations and Access Control SCJP tips

Array. Prepared By - Rifat Shahriyar

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Chief Reader Report on Student Responses:

CSC 1351 The Twelve Hour Exam From Hell

Inheritance and Polymorphism

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

301AA - Advanced Programming [AP-2017]

Notable Enhancements in Java 8. Functional Programming with Java. Lambda Expressions in Java. How to Define a Lambda Expression? Lambda expressions

Java Magistère BFA

Super-Classes and sub-classes

More about inheritance

Java Classes, Inheritance, and Interfaces

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Parametric polymorphism and Generics

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CompuScholar, Inc. 9th - 12th grades

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Selected Java Topics

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Comparing Objects 3/14/16

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

5/23/2015. Core Java Syllabus. VikRam ShaRma

Building Java Programs. Inheritance and Polymorphism

VIRTUAL FUNCTIONS Chapter 10

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

Framework Fundamentals

Chapter 14 Abstract Classes and Interfaces

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Objects and Iterators

Transcription:

Contents Topic 07 - Generic Programming I. Introduction Example 1 User defined Generic Method: printtwice(t x) Example 2 User defined Generic Class: Pair<T> Example 3 using java.util.arraylist<e> II. Type Inference and Diamond Operator III. Type parameters IV. Pitfalls and Type Erasure V. Generic Class with more than 1 parameter, Example 2 TwoTypePair VI. Bounds for type parameter, Generic Interface, Inheritance with Generic Classes I Generic Programming - Introduction Generic Programming Generics method and class definitions which involve type parameters. Generic Programming writing code that can be reused for objects of many different types. User-defined generic classes and methods [See examples 1 and 2] There are also generic classes and methods provided in the standard Java libraries: e.g. the ArrayList generic class, the Collections.sort generic method [See example 3] Example 1 Simple Generic Method public static <T> void printtwice(t x) x is called the value parameter. T is called the type parameter. <T> means that: In the following, T is the type parameter which stands for the actual type which is known when printtwice is called. printtwice("hello"); //This time T is a string printtwice(1234); //This time T is an integer printtwice(4.0/3); //This time T is a double Output: hello hello 1234 1234 1.3333333333333333 1.3333333333333333 Last modified: 13-Nov-2018 1/6

Example 2 Simple Generic Class public Pair() //first and second automatically initialized as null public Pair(T x1, T x2) first = x1; second = x2; @Override public String tostring() return "(1)"+first+" (2)"+second; <T> is called the type parameter. <T> means that: In the following, T is the type parameter which stands for the actual type which is known when an object of this Generic is created. Pair<String> p0 = new Pair<String>(); Pair<String> p1 = new Pair<String>("hello","cheers"); Pair<Boolean> p2 = new Pair<Boolean>(true,false); Pair<Integer> p3 = new Pair<Integer>(123,456); Pair<Number> p4 = new Pair<Number>(123,456); Pair<Object> p5 = new Pair<Object>(123,"cheers"); System.out.println(p0); System.out.println(p1); System.out.println(p2); System.out.println(p3); System.out.println(p4); System.out.println(p5); Each time when we use the Generic Class Pair, we need to tell the type which T stands for. Output: (1)null (2)null (1)hello (2)cheers (1)true (2)false (1)123 (2)456 (1)123 (2)456 (1)123 (2)cheers Example 3 The ArrayList generic class, the Collections.sort generic method [provided in standard Java libraries] import java.util.arraylist; import java.util.collections; ArrayList<Integer> arrlist = new ArrayList<>(); arrlist.add(1234); arrlist.add(8899); arrlist.add(36); Collections.sort(arrlist); System.out.println(arrlist); //Output: [36, 1234, 8899] [For interested students only] The ArrayList class is defined as: class ArrayList<E> The sort method is defined as: public static <T extends Comparable<? super T>> void sort(list<t> list) Where List is a Java interface implemented by ArrayList and a number of other Java classes. [ http://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html ] [http://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java ] Last modified: 13-Nov-2018 2/6

II Type Inference and Diamond Syntax Type Inference Type inference is a Java compiler's ability to determine the type argument(s) that make the invocation applicable. Compiler looks at each method invocation and corresponding declaration, in order to decide the type. Ref: http://docs.oracle.com/javase/tutorial/java/generics/gentypeinference.html The Diamond Syntax: <> We can omit the types in <> when new is used. (Since Java 7) Example 1 public static <T> void printtwice(t x) printtwice("hello"); //This time T is a string printtwice(1234); //This time T is an integer printtwice(4.0/3); //This time T is a double Example 2.. Pair<String> p0 = new Pair<String>(); Pair<String> p1 = new Pair<String>("..", ".."); Pair<Boolean> p2 = new Pair<Boolean>(true,false); This time T is a boolean type parameter type argument i.e., simply write Pair<String> p0 = new Pair<> (); Pair<String> p1 = new Pair<> ("hello","cheers"); Pair<Boolean> p2 = new Pair<>(true,false); Compiler checks the type of the object variable (here p0, p1, p2) to guess and fill in the type parameter. III Type Parameters Type parameters A type parameter can be plugged in with any reference type (ie. reference type to an object of a class) ArrayList<int> arrlist = new ArrayList<>(); ArrayList<Integer> arrlist = new ArrayList<>(); The type parameter can be used as a type in the code of the generic class / method. When a specific type is plugged in, this produces a specific class type or method. This is called Instantiation, e.g. we instantiate the ArrayList generic class to create the ArrayList<Integer> class. Naming convention:.. public static <T> void printtwiceb(t x) T y=x; System.out.println(y); System.out.println(y); T Type (2 nd,3 rd,4 th types as S,U,V etc) E - Element (used extensively by Java Collections) K Key N Number V Value Other names (can be longer, should start with uppercase) are okay but may not look professional. Last modified: 13-Nov-2018 3/6

IV Pitfalls and Type Erasure Static fields belong to the generic class, not the instantiated classes. class Smartphone class Pager class TabletPC class MobileDevice<T> private static int count=0; MobileDevice() count++;system.out.println(count); //... MobileDevice<Smartphone> phone = new MobileDevice<>(); MobileDevice<Pager> pager = new MobileDevice<>(); MobileDevice<TabletPC> pc = new MobileDevice<>(); Output: 1 2 3 Cannot declare static fields of a type parameter public class MobileDevice<T> private static T os; // compile-time error //... MobileDevice<Smartphone> phone = new MobileDevice<>(); MobileDevice<Pager> pager = new MobileDevice<>(); MobileDevice<TabletPC> pc = new MobileDevice<>(); What should be the type of os? [ http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createobjects ] Cannot create an object instance of a type parameter Compile error: public static <E> void append(list<e> list) E elem = new E(); // compile-time error list.add(elem); A class cannot have two overloaded methods that will have the same signature after type erasure. public class X public void print(pair<string> strset) //compile-time error public void print(pair<integer> intset) //compile-time error Last modified: 13-Nov-2018 4/6

Type Erasure [For interested students only] http://docs.oracle.com/javase/tutorial/java/generics/erasure.html http://docs.oracle.com/javase/tutorial/java/generics/gentypes.html Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. To implement generics, the Java compiler applies type erasure to: The raw type for Pair<T> in Example 2 looks like: class Pair private Object first; private Object second; public Pair() Replace all type parameters in generic types with raw types, which are their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods. public Pair(Object first, Object second) this.first = first; this.second = second; @Override public String tostring() return "(1)"+first+" (2)"+second; Insert type casts if necessary to preserve type safety. Generate bridge methods to preserve polymorphism in extended generic types. Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead. Pair p0 = new Pair(); Pair p1 = new Pair("hello","cheers"); Pair p5 = new Pair(123,"cheers"); System.out.println(p0); //(1)null (2)null System.out.println(p1); //(1)hello (2)cheers System.out.println(p5); //(1)123 (2)cheers Pitfalls - More [For interested students only] public Pair() public Pair(T x1, T x2) first = x1; second = x2; Constructor headings do not include the type parameter We do not write public <T> Pair() public <T> Pair(T x1, T x2) first = x1; second = x2; Cannot use a generic class as the base type of an array String [] x1=new String[3]; //OK Pair<String>[] x2 = new Pair<String>[3]; //Error: Cannot create a generic array of Pair<String> Cannot create, catch, or throw objects of parameterized types class MathException<T> extends Exception /*... */ // compile-time error class QueueFullException<T> extends Throwable /*... */ // compile-time error Cannot use casts or instanceof with parameterized types if (list instanceof ArrayList<Integer>) // compile-time error Pair<Integer> li = new Pair<>(); Pair<Number> ln = (Pair<Number>) li; // compile-time error Last modified: 13-Nov-2018 5/6

V Generic Class with more than 1 parameter, Example 2 TwoTypePair Generic Class can have more than 1 type parameters class TwoTypePair<T1, T2> private T1 first; private T2 second; Example 2a public TwoTypePair(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; public boolean equals(object otherobject) if (otherobject == null) return false; else if (getclass( )!= otherobject.getclass( )) return false; else TwoTypePair<T1, T2> otherpair = (TwoTypePair<T1, T2>)otherObject; return (first.equals(otherpair.first) && second.equals(otherpair.second)); TwoTypePair<String, Integer> x1,x2,x3; x1 = new TwoTypePair<>("USD",123); x2 = new TwoTypePair<>("USD",456); x3 = new TwoTypePair<>("USD",123); System.out.println(x1.equals(x2));//false System.out.println(x1.equals(x3));//true VI Other Notes - Bounds for type parameter, Generic Interface, Inheritance with Generic Class Bounds for Type Parameters To restrict the possible types that can be plugged in for a type parameter T Example: public class TwoBTypePair<T1 extends Class1, T2 extends Class2 & Comparable> Generic Interfaces (Similar to generic classes) Example: interface PairInterface<T1, T2> void processthepair(t1 t1, T2 t2); Inheritance with Generic Classes A generic class can be defined as a derived class of an ordinary class or of another generic class Example: class C0 class C1<T> extends C0 class C2<T> extends C1<T> class C3<T> extends C1<String> Last modified: 13-Nov-2018 6/6