SlideShare a Scribd company logo
Agenda
 Generic Methods
 Generic Classes
 Generics and Inheritance
 Erasure
1
2
Stack interfaces
interface StringStack{
void push(String s);
String pop();
}
interface IntegerStack{
void push(Integer s);
Integer pop();
}
interface StudentStack{...
3
Sort Method
static void sort(Integer[] array) {
// ...
}
static void sort(Double[] array) {
// ...
}
static void sort(String[] array) {
// ...
}
static void sort(Student[] array){
// ...
}
4
The Problem
 What is wrong with these examples?
 Code redundancy
 No effective code reuse
 Solution?
 Using Object class
 Compile-time type safety
5
The Solution
 Generic types and methods
 Methods with similar implementation
 Applicable for different parameters
6
Generic Methods
 Declaring a method which accepts different parameter
types
 For each method invocation, the compiler searches the
appropriate method
 If the compiler does not find a method, it looks for a
compatible generic method
7
Type Parameter
It says: In this method, E is not a regular type,
it is a generic one
printArray() Generic Method
8
Benefits of Generics
public static < E extends Number>
void printArray( E[] inputArray ){…}
 Restricting possible types
 Compile-time type checking
 printArray(stringArray) brings
 Compiler Error
 or exception?
9
Type parameter as the Return Type
10
Stack Generic Interface
interface Stack<T>{
void push(T s);
T pop();
}
Stack<String> stringStack = new ...
stringStack.push(“salam”);
String s = stringStack.pop();
11
public class Stack<E > {
private E[] elements ;
private final int size; // number of elements in the stack
private int top; // location of the top element
public void push(E pushValue) {
if (top == size - 1) // if stack is full
throw new FullStackException();
elements[++top] = pushValue;
}
public E pop() {
if (top == -1) // if stack is empty
throw new EmptyStackException();
return elements[top--];
}
public Stack() {
size = 10;
top = -1;
elements = (E[]) new Object[size];
}
}
12
A note, later….
Using Stack Class
Stack<String> stack1 = new Stack<String>();
stack1.push("first");
stack1.push("second");
System.out.println(stack1.pop());
System.out.println(stack1.pop());
Stack<Integer> stack2 = new Stack<Integer>();
stack2.push(1);
stack2.push(2);
System.out.println(stack2.pop());
System.out.println(stack2.pop());
13
Compile-time Type Checking
Stack<String> stack1 = new Stack<String>();
stack1.push(new Integer(2));
 Compile-time error
14
public class Stack<E extends Student> {
private E[] elements ;
private final int size; // number of elements in the stack
private int top; // location of the top element
public void push(E pushValue) {
if (top == size - 1) // if stack is full
throw new FullStackException();
elements[++top] = pushValue;
}
public E pop() {
if (top == -1) // if stack is empty
throw new EmptyStackException();
return elements[top--];
}
public Stack() {
size = 10;
top = -1;
elements = (E[]) new Student[size];
}
} 15
A note, later….
Raw Types
 Generic classes and methods can be used without type
parameter
 Stack<String> s = new Stack<String>();
 String as type parameter
 s.push(“salam”);
 s.push(new Integer(12));  Compiler Error
 Stack objectStack = new Stack();
 no type parameter
 s.push(“salam”);
 s.push(new Integer(12));
 s.push(new Student(“Ali Alavi”));
16
No Generics in Runtime
 Generics is a compile-time aspect
 In runtime, there is no generic information
 All generic classes and methods are translated with
raw types
 Byte code has no information about generics
 Only raw types in byte code
 This mechanism is named erasure
17
Erasure
 When the compiler translates generic method into
Java bytecodes
 It removes the type parameter section
 It replaces the type parameters with actual types.
 This process is known as erasure
18
Erasure Example (1)
class Stack<T>{
void push(T s){...}
T pop() {...}
}
 Is translated to
class Stack {
void push(Object s){...}
Object pop() {...}
}
19
Erasure Example (2)
 Translated to
20
What Happens if…
public static <E extends Number> void f(E i){
}
public static void f(Number i){
}
 Compiler Error : Method f(Number) has the same
erasure f(Number) as another method in this type
21
Generics and Inheritance
 A non-generic class can be inherited by a non-generic class
 As we saw before learning generics
 A generic class can be inherited from a non-generic class
 Adding generality to classes
 A non-generic class can be inherited from a generic class
 Removing generality
 A generic class can be inherited by a generic class
22
class GenericList<T> extends Object{
public void add(T t){...}
public T get(int i) {...}
public void remove(int i) {...}
}
class GenericNumericList<T extends Number>
extends GenericList<T>{
}
class NonZeroIntegerList
extends GenericList<Integer>{
public void add(Integer t) {
if(t==null || t==0)
throw new RuntimeException(“Bad value");
super.add(t);
}
}
23
Some Notes
 We can also create generic interfaces
interface Stack<T>{
void push(T s);
T pop();
}
 No primitives as type parameters
24
Multiple Type Parameters
class MultipleType<T,K>{
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public void doSomthing(K k, T t){…}
}
MultipleType<String, Integer> multiple =
new MultipleType<String, Integer>();
multiple.doSomthing(5, "123");
25
Note
 You can not instantiate generic classes
class Stack<T>{
T ref = new T();
}
 Syntax Error: Cannot instantiate the type T
 Why?
26
Note (2)
 You can not instantiate generic classes
class Stack<T>{
T[] elements = new T[size];
}
 Syntax Error: Cannot instantiate the type T
 Why?
27
Note (3)
 You cannot create a generic array
class Box<T> {
final T x;
Box(T x) {
this.x = x;
}
}
 Then, this line brings a compile error:
Box<String>[] bsa = new Box<String>[3];
 Why?
28
Syntax Error:
Cannot create a generic array of Box<String>
Reason
 Operations such as instanceof and new are runtime
operations
 They use a type at runtime
 With erasure type information is removed at runtime
 So these operations are Meaningless
 Although, they may be possible
29
T ref = new T();  impossible
• which constructor?
T[] elements = new T[size];  Meaningless
Box<String>[] bsa = new Box<String>[3];  Meaningless
Generics and Java 7
 Older versions:
ArrayList<String> list = new ArrayList<String>();
 With Java 7:
ArrayList<String> list = new ArrayList<>();
 Type information after new are ignored.
List<Map<Long, Set<Integer>>> list = new ArrayList<>();
30
Further Reading
 Wildcards as type parameters
 Java generics vs. C++ templates
 Erasure is different in these languages
 Type Argument inference
 More on erasure
 TIJ is so better than Deitel in generics chapter
 More Depth
31
Wow!!!
public static void wow(ArrayList<String> list) {
Method method = list.getClass().getMethod("add",
Object.class);
method.invoke(list, new Integer(2));
}
public static void main(String args[]) {
ArrayList<String> s = new ArrayList<String>();
wow(s);
for (Object string : s) {
System.out.println(string);
}
}
32
A Note on Inheritance
class A{
public Object f(Object o){
return new Object();
}
}
class B extends A{
public Object f(Object o){
return new String(“hello");
}
}
 B.f() overrides A.f()
33
A Note on Inheritance
class A{
public Object f(Object o){
return new Object();
}
}
class B extends A{
public String f(Object o){
return new String("salam");
}
}
 B.f() overrides A.f()
34
A Note on Inheritance
class A{
public Object f(Object o){
return new Object();
}
}
class B extends A{
public Object f(String o){
return new String("salam");
}
}
 B.f() is overloading A.f()
 B.f() does not override A.f()
35
Pair class (Quiz)
 Pair<T, K>
 equals
 toString
36
class Pair<T,K>{
private T first;
private K second;
public Pair(T t, K k) {
this.first = t;
this.second = k;
}
public T getFirst() {
return first;
}
public K getSecond() {
return second;
}
public String toString() {
return "[" + second + ", " + first + "]";
}
}
37
Pair<Integer, String> pair1 =
new Pair<Integer, String>(4, "Ali");
Integer i = pair1.getFirst();
String s = pair1.getSecond();
Pair<String, Boolean> pair2 =
new Pair<String, Boolean>("salam", true);
String ss = pair2.getFirst();
Boolean bb = pair2.getSecond();
38
equals() method
public boolean equals(Pair<T,K> pair) {
return
pair.first.equals(first)
&&
pair.second.equals(second);
}
 What is wrong with this implementation?
39
boolean equals(Pair<T,K> pair)
 It should check for nullity of pair
 It should check for nullity of pair.first and pair.second
 It should check for nullity of this.first and this.second
 This method does not override equals()
 It is overloading it
 Correct signature:
boolean equals(Object pair)
 What if parameter is not a Pair?
40
Type Checking
public boolean equals(Object o) {
Pair<T, K> pair = null;
try{
pair = (Pair<T, K>) o;
}catch(ClassCastException e){
return false;
}
return pair.first.equals(first) &&
pair.second.equals(second);
}
41
42
43

More Related Content

PPTX
Refactoring and code smells
Paul Nguyen
 
PPTX
Code smells and remedies
Md.Mojibul Hoque
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
PPTX
Java loops for, while and do...while
Jayfee Ramos
 
PPTX
9. ES6 | Let And Const | TypeScript | JavaScript
pcnmtutorials
 
PPT
Google mock for dummies
Luis Goldster
 
PPT
Packages in java
Abhishek Khune
 
PPT
Methods in C#
Prasanna Kumar SM
 
Refactoring and code smells
Paul Nguyen
 
Code smells and remedies
Md.Mojibul Hoque
 
Inheritance C#
Raghuveer Guthikonda
 
Java loops for, while and do...while
Jayfee Ramos
 
9. ES6 | Let And Const | TypeScript | JavaScript
pcnmtutorials
 
Google mock for dummies
Luis Goldster
 
Packages in java
Abhishek Khune
 
Methods in C#
Prasanna Kumar SM
 

What's hot (20)

PPT
Java packages
Raja Sekhar
 
PPTX
inheritance
Jay Prajapati
 
PDF
C# conventions & good practices
Tan Tran
 
PDF
Refactoring
Ricardo Terra
 
PPTX
Coding conventions
systemcrashed
 
PPTX
Array in c#
Prem Kumar Badri
 
PDF
Structure In C
yndaravind
 
PDF
Advanced Java Practical File
Soumya Behera
 
PDF
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Philip Schwarz
 
PPTX
Recursion in c++
Abdul Rehman
 
PPT
JAVA OOP
Sunil OS
 
PPTX
Java Foundations: Lists, ArrayList<T>
Svetlin Nakov
 
PPT
Generics in java
suraj pandey
 
PPT
Java: GUI
Tareq Hasan
 
DOCX
Recursion in C++
Maliha Mehr
 
PPTX
Methods In C-Sharp (C#)
Abid Kohistani
 
PDF
Clean coding-practices
John Ferguson Smart Limited
 
PPTX
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
PPT
C# Basics
Sunil OS
 
PPT
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Java packages
Raja Sekhar
 
inheritance
Jay Prajapati
 
C# conventions & good practices
Tan Tran
 
Refactoring
Ricardo Terra
 
Coding conventions
systemcrashed
 
Array in c#
Prem Kumar Badri
 
Structure In C
yndaravind
 
Advanced Java Practical File
Soumya Behera
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Philip Schwarz
 
Recursion in c++
Abdul Rehman
 
JAVA OOP
Sunil OS
 
Java Foundations: Lists, ArrayList<T>
Svetlin Nakov
 
Generics in java
suraj pandey
 
Java: GUI
Tareq Hasan
 
Recursion in C++
Maliha Mehr
 
Methods In C-Sharp (C#)
Abid Kohistani
 
Clean coding-practices
John Ferguson Smart Limited
 
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
C# Basics
Sunil OS
 
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Ad

Similar to Java generics (20)

PPT
Java Generics
jeslie
 
PDF
Java Class Design
Ganesh Samarthyam
 
PDF
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
PDF
Java Generics - by Example
CodeOps Technologies LLP
 
PDF
Java Generics - by Example
Ganesh Samarthyam
 
PPT
Generic Types in Java (for ArtClub @ArtBrains Software)
Andrew Petryk
 
PPTX
Java arrays
Maneesha Caldera
 
PDF
Lec 6 14_aug [compatibility mode]
Palak Sanghani
 
PDF
Lezione03
robynho86
 
PDF
Lezione03
robynho86
 
PDF
I only need help with four methods in the EmployeeManager class the .pdf
arpitcomputronics
 
PPT
Java Generics for Dummies
knutmork
 
PPT
Csharp4 generics
Abed Bukhari
 
PPTX
Java New Programming Features
tarun308
 
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
asarudheen07
 
PDF
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 
PPTX
Chapter 6.6
sotlsoc
 
PPTX
JDK 8
Maciej Dragan
 
PPT
Csharp In Detail Part2
Mohamed Krar
 
PPT
Jeop game-final-review
Stephanie Weirich
 
Java Generics
jeslie
 
Java Class Design
Ganesh Samarthyam
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
Java Generics - by Example
CodeOps Technologies LLP
 
Java Generics - by Example
Ganesh Samarthyam
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Andrew Petryk
 
Java arrays
Maneesha Caldera
 
Lec 6 14_aug [compatibility mode]
Palak Sanghani
 
Lezione03
robynho86
 
Lezione03
robynho86
 
I only need help with four methods in the EmployeeManager class the .pdf
arpitcomputronics
 
Java Generics for Dummies
knutmork
 
Csharp4 generics
Abed Bukhari
 
Java New Programming Features
tarun308
 
import java-util--- public class MyLinkedList{ public static void.pdf
asarudheen07
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 
Chapter 6.6
sotlsoc
 
Csharp In Detail Part2
Mohamed Krar
 
Jeop game-final-review
Stephanie Weirich
 
Ad

Java generics

  • 1. Agenda  Generic Methods  Generic Classes  Generics and Inheritance  Erasure 1
  • 2. 2
  • 3. Stack interfaces interface StringStack{ void push(String s); String pop(); } interface IntegerStack{ void push(Integer s); Integer pop(); } interface StudentStack{... 3
  • 4. Sort Method static void sort(Integer[] array) { // ... } static void sort(Double[] array) { // ... } static void sort(String[] array) { // ... } static void sort(Student[] array){ // ... } 4
  • 5. The Problem  What is wrong with these examples?  Code redundancy  No effective code reuse  Solution?  Using Object class  Compile-time type safety 5
  • 6. The Solution  Generic types and methods  Methods with similar implementation  Applicable for different parameters 6
  • 7. Generic Methods  Declaring a method which accepts different parameter types  For each method invocation, the compiler searches the appropriate method  If the compiler does not find a method, it looks for a compatible generic method 7 Type Parameter It says: In this method, E is not a regular type, it is a generic one
  • 9. Benefits of Generics public static < E extends Number> void printArray( E[] inputArray ){…}  Restricting possible types  Compile-time type checking  printArray(stringArray) brings  Compiler Error  or exception? 9
  • 10. Type parameter as the Return Type 10
  • 11. Stack Generic Interface interface Stack<T>{ void push(T s); T pop(); } Stack<String> stringStack = new ... stringStack.push(“salam”); String s = stringStack.pop(); 11
  • 12. public class Stack<E > { private E[] elements ; private final int size; // number of elements in the stack private int top; // location of the top element public void push(E pushValue) { if (top == size - 1) // if stack is full throw new FullStackException(); elements[++top] = pushValue; } public E pop() { if (top == -1) // if stack is empty throw new EmptyStackException(); return elements[top--]; } public Stack() { size = 10; top = -1; elements = (E[]) new Object[size]; } } 12 A note, later….
  • 13. Using Stack Class Stack<String> stack1 = new Stack<String>(); stack1.push("first"); stack1.push("second"); System.out.println(stack1.pop()); System.out.println(stack1.pop()); Stack<Integer> stack2 = new Stack<Integer>(); stack2.push(1); stack2.push(2); System.out.println(stack2.pop()); System.out.println(stack2.pop()); 13
  • 14. Compile-time Type Checking Stack<String> stack1 = new Stack<String>(); stack1.push(new Integer(2));  Compile-time error 14
  • 15. public class Stack<E extends Student> { private E[] elements ; private final int size; // number of elements in the stack private int top; // location of the top element public void push(E pushValue) { if (top == size - 1) // if stack is full throw new FullStackException(); elements[++top] = pushValue; } public E pop() { if (top == -1) // if stack is empty throw new EmptyStackException(); return elements[top--]; } public Stack() { size = 10; top = -1; elements = (E[]) new Student[size]; } } 15 A note, later….
  • 16. Raw Types  Generic classes and methods can be used without type parameter  Stack<String> s = new Stack<String>();  String as type parameter  s.push(“salam”);  s.push(new Integer(12));  Compiler Error  Stack objectStack = new Stack();  no type parameter  s.push(“salam”);  s.push(new Integer(12));  s.push(new Student(“Ali Alavi”)); 16
  • 17. No Generics in Runtime  Generics is a compile-time aspect  In runtime, there is no generic information  All generic classes and methods are translated with raw types  Byte code has no information about generics  Only raw types in byte code  This mechanism is named erasure 17
  • 18. Erasure  When the compiler translates generic method into Java bytecodes  It removes the type parameter section  It replaces the type parameters with actual types.  This process is known as erasure 18
  • 19. Erasure Example (1) class Stack<T>{ void push(T s){...} T pop() {...} }  Is translated to class Stack { void push(Object s){...} Object pop() {...} } 19
  • 20. Erasure Example (2)  Translated to 20
  • 21. What Happens if… public static <E extends Number> void f(E i){ } public static void f(Number i){ }  Compiler Error : Method f(Number) has the same erasure f(Number) as another method in this type 21
  • 22. Generics and Inheritance  A non-generic class can be inherited by a non-generic class  As we saw before learning generics  A generic class can be inherited from a non-generic class  Adding generality to classes  A non-generic class can be inherited from a generic class  Removing generality  A generic class can be inherited by a generic class 22
  • 23. class GenericList<T> extends Object{ public void add(T t){...} public T get(int i) {...} public void remove(int i) {...} } class GenericNumericList<T extends Number> extends GenericList<T>{ } class NonZeroIntegerList extends GenericList<Integer>{ public void add(Integer t) { if(t==null || t==0) throw new RuntimeException(“Bad value"); super.add(t); } } 23
  • 24. Some Notes  We can also create generic interfaces interface Stack<T>{ void push(T s); T pop(); }  No primitives as type parameters 24
  • 25. Multiple Type Parameters class MultipleType<T,K>{ private T t; public T getT() { return t; } public void setT(T t) { this.t = t; } public void doSomthing(K k, T t){…} } MultipleType<String, Integer> multiple = new MultipleType<String, Integer>(); multiple.doSomthing(5, "123"); 25
  • 26. Note  You can not instantiate generic classes class Stack<T>{ T ref = new T(); }  Syntax Error: Cannot instantiate the type T  Why? 26
  • 27. Note (2)  You can not instantiate generic classes class Stack<T>{ T[] elements = new T[size]; }  Syntax Error: Cannot instantiate the type T  Why? 27
  • 28. Note (3)  You cannot create a generic array class Box<T> { final T x; Box(T x) { this.x = x; } }  Then, this line brings a compile error: Box<String>[] bsa = new Box<String>[3];  Why? 28 Syntax Error: Cannot create a generic array of Box<String>
  • 29. Reason  Operations such as instanceof and new are runtime operations  They use a type at runtime  With erasure type information is removed at runtime  So these operations are Meaningless  Although, they may be possible 29 T ref = new T();  impossible • which constructor? T[] elements = new T[size];  Meaningless Box<String>[] bsa = new Box<String>[3];  Meaningless
  • 30. Generics and Java 7  Older versions: ArrayList<String> list = new ArrayList<String>();  With Java 7: ArrayList<String> list = new ArrayList<>();  Type information after new are ignored. List<Map<Long, Set<Integer>>> list = new ArrayList<>(); 30
  • 31. Further Reading  Wildcards as type parameters  Java generics vs. C++ templates  Erasure is different in these languages  Type Argument inference  More on erasure  TIJ is so better than Deitel in generics chapter  More Depth 31
  • 32. Wow!!! public static void wow(ArrayList<String> list) { Method method = list.getClass().getMethod("add", Object.class); method.invoke(list, new Integer(2)); } public static void main(String args[]) { ArrayList<String> s = new ArrayList<String>(); wow(s); for (Object string : s) { System.out.println(string); } } 32
  • 33. A Note on Inheritance class A{ public Object f(Object o){ return new Object(); } } class B extends A{ public Object f(Object o){ return new String(“hello"); } }  B.f() overrides A.f() 33
  • 34. A Note on Inheritance class A{ public Object f(Object o){ return new Object(); } } class B extends A{ public String f(Object o){ return new String("salam"); } }  B.f() overrides A.f() 34
  • 35. A Note on Inheritance class A{ public Object f(Object o){ return new Object(); } } class B extends A{ public Object f(String o){ return new String("salam"); } }  B.f() is overloading A.f()  B.f() does not override A.f() 35
  • 36. Pair class (Quiz)  Pair<T, K>  equals  toString 36
  • 37. class Pair<T,K>{ private T first; private K second; public Pair(T t, K k) { this.first = t; this.second = k; } public T getFirst() { return first; } public K getSecond() { return second; } public String toString() { return "[" + second + ", " + first + "]"; } } 37
  • 38. Pair<Integer, String> pair1 = new Pair<Integer, String>(4, "Ali"); Integer i = pair1.getFirst(); String s = pair1.getSecond(); Pair<String, Boolean> pair2 = new Pair<String, Boolean>("salam", true); String ss = pair2.getFirst(); Boolean bb = pair2.getSecond(); 38
  • 39. equals() method public boolean equals(Pair<T,K> pair) { return pair.first.equals(first) && pair.second.equals(second); }  What is wrong with this implementation? 39
  • 40. boolean equals(Pair<T,K> pair)  It should check for nullity of pair  It should check for nullity of pair.first and pair.second  It should check for nullity of this.first and this.second  This method does not override equals()  It is overloading it  Correct signature: boolean equals(Object pair)  What if parameter is not a Pair? 40
  • 41. Type Checking public boolean equals(Object o) { Pair<T, K> pair = null; try{ pair = (Pair<T, K>) o; }catch(ClassCastException e){ return false; } return pair.first.equals(first) && pair.second.equals(second); } 41
  • 42. 42
  • 43. 43