Generic Types  Recent release of Java added generics  Include type parameters in class definition  Like methods, parameters can change each time 

Slides:



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

1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
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
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.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Java Generics.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Introduction to Object-Oriented Programming
1-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Object Oriented Programming … and other things you need to program in java.
Lecture :2 1.  DEFENTION : Java is a programming language expressly designed for use in the distributed environment of the Internet. It was designed.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics -- How does an atom work?
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Question of the Day  A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
CSI 3125, Preliminaries, page 1 Compiling the Program.
Pointers *, &, array similarities, functions, sizeof.
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.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSE 1201 Object Oriented Programming ArrayList 1.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
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.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
PHY 107 – Programming For Science. Announcements  No week #5 weekly assignment  Take time to study for Friday’s midterm instead  No lab or office hours.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Question of the Day  Move one matchstick to produce a square.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Problem of the Day  Why are manhole covers round?
Using local variable without initialization is an error.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Dynamic Data Structures and Generics
Generics.
Java Programming Language
Introduction to Object-Oriented Programming
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Presentation transcript:

Generic Types  Recent release of Java added generics  Include type parameters in class definition  Like methods, parameters can change each time  Fields independent of types can now be written

Generic Types  On allocating instance, actual type is specified  Must be reference type or String as actual type  Code runs as if were written using that type  Type used by instance cannot be changed  Type parameter becomes part of variable’s type

Generics Before & After Before GenericsAfter Generics

Writing Generics

See Generics Behave

For earth, class written like as if T were replaced by Integer

See Generics Behave

For matchbox, T is Car at same time it is Integer when using earth

Using Generics Without GenericsWith Generics Integer i; Car c; Bag n;... n = new Bag(5,“B”); i = ((Integer)n.getData()); c = ((Car)n.getData()); n.setData(c); i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i; Car c; Bag n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(s); m = new Bag (c, “B”); i = m.getData(); c = m.getData();

Using Generics Without GenericsWith Generics Integer i; Car c; Bag n;... n = new Bag(5,“B”); i = ((Integer)n.getData()); c = ((Car)n.getData()); n.setData(c); i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i; Car c; Bag n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(s); m = new Bag (c, “B”); i = m.getData(); c = m.getData();

Using Generics Without GenericsWith Generics Integer i; Car c; Bag n;... n = new Bag(5,“B”); i = ((Integer)n.getData()); c = ((Car)n.getData()); n.setData(c); i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i; Car c; Bag n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(s); m = new Bag (c, “B”); i = m.getData(); c = m.getData();

Using Generics Without GenericsWith Generics Integer i; Car c; Bag n;... n = new Bag(5,“B”); i = ((Integer)n.getData()); c = ((Car)n.getData()); n.setData(c); i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i; Car c; Bag n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();

public class Entry { private S key; private TYPE value; // And more goes here... } Entry a; Entry b; Entry c; Entry d; Entry e; Can Use Multiple Generic Types

public class Entry { private S key; private TYPE value; // And more goes here... } Entry a; Entry b; Did not specify for each type Entry c; Entry d; Entry e; Can Use Multiple Generic Types

public class Entry { private S key; private TYPE value; // And more goes here... } Entry a; Entry b; Did not specify for each type Entry c; Entry d; Not reference type Entry e; Can Use Multiple Generic Types

When To Specify Type  Whenever class name used (except constructors)  Variable declarations: ArrayList hogCount;  Object instantiation: hogCount = new ArrayList ();  Return type for method : private ArrayList transport()  Parameter listing: public void cook(ArrayList fd)  Used as type parameter: ArrayList > bacon;

 Type cannot be specified instantiating array  Compiler error if type specified during instantiation  Can provide type theory explaining this problem Generics Annoyance

 Type cannot be specified instantiating array  Can use generics with arrays, but need typecast  Only needed once, use generics after instantiation  Still checks when compiling, so get most benefits public class Farm { private ArrayList [] troughs; private T[] animals; public Farm() { troughs = (ArrayList [])new ArrayList[10]; animals = (T[])new Object[ ]; } } Generics Annoyance

In Case of Unknown Type

Wildcard in Generic public class ListHolder { private ArrayList myList; public void setList(ArrayList lst){ myList = lst; } public void printListSize() { System.out.println(myList.size()); } public ArrayList getList() { return myList; } }

Wildcard in Generic public class ListHolder { private ArrayList myList; public void setList(ArrayList lst){ myList = lst; } public void printListSize() { System.out.println(myList.size()); } public ArrayList getList() { return myList; } } ? matches any reference type (and String )

Wildcard in Generic public class ListHolder { private ArrayList myList; public void setList(ArrayList lst){ myList = lst; } public void printListSize() { System.out.println(myList.size()); } public ArrayList getList() { return myList; } } Any ArrayList can be passed in for lst

Wildcard in Generic public class ListHolder { private ArrayList myList; public void setList(ArrayList lst){ myList = lst; } public void printListSize() { System.out.println(myList.size()); } public ArrayList getList() { return myList; } } Can call methods as long as missing type not important

Wildcard in Generic public class ListHolder { private ArrayList myList; public void setList(ArrayList lst){ myList = lst; } public void printListSize() { System.out.println(myList.size()); } public ArrayList getList() { return myList; } } Legal, but yucky. All type information is lost!

Typecasting Explained

Your Turn  Get into your groups and complete activity

For Monday  You can use on this midterm:  Your textbook & notes IF  Printout of slides IF has notes on that day's slides  At the same time, you may NOT use:  Computer, calculator, cell phone, or similar  Copies of daily activities and/or solutions  Friends, Romans, countrymen or their ears  To be certain rules are followed, when test ends  Hand in all printed material you had with you

How to Prepare for Monday DODON'T  Make cheat sheets for the test  Review how parts of Java work  Add post-its to important pages  Memorize  Drink case of 40s before test  Use post-its as clothing

For Next Lecture  Midterm #1 is on Monday  Study, study, study  Get your notes in order