0% found this document useful (0 votes)
12 views11 pages

CS239 - Lecture 29 - Generics, V2

Uploaded by

hamidyaseen1907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

CS239 - Lecture 29 - Generics, V2

Uploaded by

hamidyaseen1907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Starting Out with Java

From Control Structures through Objects

Lecture No. 29 and 30: Generics in Java

May 18, 2021

1 / 11
Generics in Java

Generics in Java is similar to templates in C++.

Generics allow you to abstract over types.

Generic Methods: Enable programmers to specify, with a


single method declaration, a set of related methods.

Generic Classes: Allow programmers to specify, with a single


class declaration, a set of related types.

2 / 11
Generic Methods

Generic Methods: Writing a single generic method declaration


that can be called with arguments of dierent types (e.g.
String, Double, Integer).

Compiler checks the generic method's arguement type to


handle each method call appropriately.

3 / 11
Generic Methods

Generic Method Example:


// A Generic method example
static <T> void genericDisplay (T element) {
System.out.println(element.getClass().getName() +
" = " + element);
}

Type Parameter section delimited by angled brakets (<T >).

A type parameter (T ), also known as a type variable, is an


identier that species a generic type name.

4 / 11
Generic Methods

Example:
class GenericMethods {

// Generic Method
public static <T> void printArray(T[] array) {
for(T value : array)
System.out.print(value + ", ");
System.out.println();
}//printArray

public static void main(String[] args) {

Integer[] iiarray = {10, 20, 30, 40, 50};


Double[] ddarray = {10.1, 20.2, 30.3, 40.4, 50.5};
Character[] ccarray = {'?', '+', '<', '>', '@'};
String[] sarray = {"A", "a", "B", "b"};

printArray(iiarray);
printArray(ddarray);
printArray(ccarray);
printArray(sarray);
}//main

}//class 5 / 11
Example: Generic Methods

Example is provided in the lecture!

6 / 11
Generic Classes

Generic Classes: A generic class declaration looks like a


non-generic class declaration, except that the class name is
followed by a type parameter section (<>).

Generic classes are known as parameterized classes or


parameterized types because they accept one or more
parameters.

7 / 11
Generic Class

Generic Class Example:


// Generic Class
class Box<T> {

T side;

Box(T side) {
this.setSide(side);
}

void setSide(T side) {


this.side = side;
}//setSide

T getSide() {
return this.side;
}//getSide

public String toString() {


return("[Side = " + this.getSide() + "]");
}//toString

}//class
8 / 11
Generic Class

Generic Class Example:


class TestBox {

public static void main(String[] args) {


Box<Integer> b = new Box<Integer>(100);
System.out.println(b);

Box<Double> d = new Box<Double>(200.99);


System.out.println(d);

Box<Character> c = new Box<>('+');


System.out.println(c);

Box<String> s = new Box<>("GIFT University");


System.out.println(s);

}//main

}//class

9 / 11
Example: Generic Classes

Example is provided in the lecture!

10 / 11
Credit

Tony Gaddis, Starting out with Java: From Control Structures

through Objects, 6th Edition, Pearson 2016.

11 / 11

You might also like