Lecture 10 Generic
Lecture 10 Generic
Programming
IT069IU
Lecture 10
Generic Classes and Methods
types, respectively.
2
Motivation for Generic Methods
Overloaded methods are often used to perform similar
4
Motivation for Generic Methods
5
Motivation for Generic Methods
6
Generic Methods: Implementation and
Complie-Time Translation
If the operations performed by several overloaded methods are
identical for each argument type, the overloaded methods can
be more compactly and conveniently coded using a generic-
method.
You can write a single generic method declaration that can be
called with arguments of different types.
Based on the types of the arguments passed to the generic
method, the compiler handles each method call appropriately.
Line 22 begins method printArray’s declaration.
7
Generic Methods: Implementation and
Complie-Time Translation
8
Generic Methods: Implementation and
Complie-Time Translation
9
Java Generic Type
N - Number
T - Type
11
Java Generic Method
class Test {
// A Generic method example
static <T> void genericDisplay(T element)
{
System.out.println(element.getClass().getName()
+ " = " + element);
}
123456789);
String name = entry.getKey();
Integer id = entry.getValue();
123456789
}
}
14
Java Generic Classes and Inheritance
Example 1:
Example 3:
public class ContactEntry3<K, V> extends KeyValuePair<K, V> {
} 16
Java Generic Classes and Inheritance
Example 4:
public class ContactEntry4<K, V, T> extends KeyValuePair<K, V> {
private T obj;
public T getObj() {
return obj;
}
Return obj
}
18
Java Generic Classes and Interface
Interface:
}
}
19
Java Generic Array
public class GenericArray<T> {
private T[] array;
21
Java Generic Wildcard
Unbounded Wildcards: GenericType<?>
– represents a list of any type
import java.util.*;
public class Unbound {
public static void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("ArrayList Collection");
printCollection(collection);
Collection<String> collection2 = new LinkedList<>();
collection2.add("LinkedList Collection");
printCollection(collection2);
Collection<String> collection3 = new HashSet<>();
collection3.add("HashSet Collection");
printCollection(collection3);
}
} 22
Java Generic Wildcard
Upper Bounded Wildcards: GenericType<? extends SuperClass>
– specify that a method accepts a type and all its subclasses.
import java.util.*;
public class UpperBound {
static void processElements(List<? extends Number> a) {
for (Object element : a) {
System.out.println(element);
}}
public static void main(String[] args) {
// ArrayList Containing Integers
List<Integer> a1 = new ArrayList<>();
a1.add(10);
a1.add(20);
a1.add(30);
processElements(a1);
// ArrayList Containing Longs
List<Long> a2 = new ArrayList<>();
a2.add(100L);
a2.add(200L);
a2.add(300L);
23
processElements(a2);
Java Generic Wildcard
// Arraylist containing Doubles
List<Double> a3 = new ArrayList<>();
a3.add(21.35);
a3.add(56.47);
a3.add(78.12);
processElements(a3);
// Arraylist containing Strings
List<String> a4 = new ArrayList<>();
a4.add("One");
a4.add("Two");
a4.add("Three");
// This will not work
//processElements(a4); // Compile time
error
}
}
24
Java Generic Wildcard
Lower Bounded Wildcards: GenericType<? super SubClass>
– specify that a method accepts a type and all its superclasses
public class LowerBound {
static void processElements(List<? super Integer> a) {
for (Object element : a) {
System.out.println(element);
}}
public static void main(String[] args) {
// ArrayList Containing Integers
List<Integer> a1 = new ArrayList<>();
a1.add(10);
a1.add(20);
a1.add(30);
processElements(a1);
// ArrayList Containing Longs
List<Long> a2 = new ArrayList<>();
a2.add(100L);
a2.add(200L);
a2.add(300L);
//processElements(a2); // compiler error 25
}}