Question of the Day  Move one matchstick to produce a square.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
Advertisements

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
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
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.
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
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.
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.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
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.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
CSC 243 – Java Programming, Spring 2013 March 12, 2013 Week 7, Generics.
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.
Object Oriented Programming … and other things you need to program in java.
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.
ArrayList, Multidimensional Arrays
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Generic Types  Recent release of Java added generics  Include type parameters in class definition  Like methods, parameters can change each time 
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Introduction to Generics
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.
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
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.
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Topics Instance variables, set and get methods Encapsulation
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.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
CSC 212 – Data Structures Lecture 6: Static and non-static members.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
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?
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Using local variable without initialization is an error.
Interfaces.
Java Programming Language
Generic programming in Java
Java Programming Language
Introduction to Object-Oriented Programming
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

Question of the Day  Move one matchstick to produce a square

Question of the Day  Move one matchstick to produce a square

CSC 212 – Data Structures

Generic Types generics  Starting with 1.5 release, Java uses 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 as if T were replaced by Integer

See Generics Behave

For matchbox, T is Car This can be at same time T is Integer when for 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(c); 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();

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();

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();

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();

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();

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 MONKEY 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 MONKEY 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 MONKEY 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 Next Lecture