Generic Instructor : Sarah Alodan. Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Java Generics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Creating Generic Classes. Introduction Java Generics were added to allow for type- safe collections and eliminate the need for burdensome, code-cluttering.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L14 (Chapter 21) Generics.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
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.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java Generics.
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.
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.
Effective Java: Generics Last Updated: Spring 2009.
ArrayList, Multidimensional Arrays
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
CIS 270—Application Development II Chapter 18-Generics.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
CSI 3125, Preliminaries, page 1 Compiling the Program.
CMSC 330: Organization of Programming Languages Java Generics.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CIS265/506 Cleveland State University – Prof. Victor Matos
John Hurley Cal State LA
Chapter 20 Generic Classes and Methods
Methods Attributes Method Modifiers ‘static’
Continuing Chapter 11 Inheritance and Polymorphism
Generics.
Chapter 9 Inheritance and Polymorphism
Chapter 19 Generics Dr. Clincy - Lecture.
Generics.
Chapter 19 Generics.
Chapter 21 Generics.
CS2011 Introduction to Programming I Methods (II)
slides created by Ethan Apter
Generics, Lambdas, Reflections
CS2013 Lecture 3 John Hurley Cal State LA.
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 21 Generics.
Unit-1 Introduction to Java
Chapter 19 Generics.
Chapter 19 Generics.
Chapter 19 Generics.
Presentation transcript:

Generic Instructor : Sarah Alodan

Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList listCourse = new ArrayList(); listCourse.add("Java Programming"); listCourse.add(12); } Problem: Compiler doesn’t know listCourse should only contain String.

Question? What We can do if we want our ListCourse has only String?

Solve the problem public class Courses { public static void main(String[] args) { ArrayList listCourse = new ArrayList (); listCourse.add("Java Programming"); listCourse.add(“12”); } Compiler now knows listCourse contains only String. No compile warning on this line.

5 No Casting Needed ArrayList list = new ArrayList (); list.add(5.5); // 5.5 is automatically converted to new Double(5.5) list.add(3.0); // 3.0 is automatically converted to new Double(3.0) Double doubleObject = list.get(0); // No casting is needed double d = list.get(1); // Automatically converted to double

6 Generic ArrayList in JDK 1.5

Generic Class Problem: I want to use the same class (or method) with objects of many different types without writing the class over and over or sacrificing type safety. Solution: Generify the class! Use a variable T to represent the input type, and write your code to operate on objects of type T in a way that does not depend on the actual value of T. Now you can instantiate the class many times, passing in different types for T. Generics just allow you to abstract over types instead of values.

First Generic Class

In the Main Method Number n1 = new Number (); Number n2 = new Number (); It is important to note that a generic class is shared by all its instances regardless of its actual generic type. Although Number and Number are two types, but there is only one class Number loaded into the JVM.

Generic Class Type Parameter : T, E Generic Class = Parametrized Class.

Generic with more than one parameter

13 Bounded Generic Type public static void main(String[] args ) { Rectangle rectangle = new Rectangle(2, 2); Circle9 circle = new Circle9(2); System.out.println("Same area? " + equalArea(rectangle, circle)); } public static boolean equalArea(E object1, E object2) { return object1.findArea() == object2.findArea(); }

14 Raw Type and Backward Compatibility // raw type GenericStack stack = new GenericStack(); This is roughly equivalent to GenericStack stack = new GenericStack ();

15 Make it Safe Max.max("Welcome", 23); // Max1.java: Find a maximum object public class Max1 { /** Return the maximum between two objects */ public static > E max(E o1, E o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }

16 Generic Types and Wildcard Types

Lab Evaluation Create your First Generic Class that has two type parameters and one method. The method will accept the two type parameter and print them to console. Create many objects of your generic class with different data types.