WHAT IS GENERICS IN JAVA?
TYPES OF JAVA GENERICS
GENERIC FUNCTIONS
ADVANTAGES OF GENERICS IN JAVA
WHY GENERICS?
www.edureka.co/java-j2ee-soa-training
www.edureka.co/java-j2ee-soa-training
WHAT IS GENERICS IN JAVA?
Generics is a term that denotes a set of
language features related to the definition and
use of Generic types and methods.
www.edureka.co/java-j2ee-soa-training
www.edureka.co/java-j2ee-soa-training
WHY JAVA GENERICS?
www.edureka.co/java-j2ee-soa-training
TYPES OF JAVA GENERICS
www.edureka.co/java-j2ee-soa-training
TYPES OF
JAVA
GENERICS
Generic Type Class
class Genericclass{
private Object x;
public void set(Object x) { this.x = x; }
public Object get() { return x; }
}
www.edureka.co/java-j2ee-soa-training
TYPES OF
JAVA
GENERICS
Generic Type Interface
interface GenericInterface<T1, T2>
{
T2 PerformExecution(T1 x);
T1 ReverseExecution(T2 x); }
class Genericclass implements
GenericInterface<String, Integer> {
public Integer PerformExecution(String x)
{//execution code }
public String ReverseExecution(Integer x)
{ //execution code }
}
www.edureka.co/java-j2ee-soa-training
TYPES OF
JAVA
GENERICS
Generic Type Method
public static <T> int countAllOccurrences(T[]
list, T element) {
int count = 0;
if (element == null) {
for ( T listElement : list )
if (listElement == null)
count++;
}
else {
for ( T listElement : list )
if (element.equals(listElement))
count++;
}
return count;
}
www.edureka.co/java-j2ee-soa-training
TYPES OF
JAVA
GENERICS
Generic Type Constructor
class Dimension<T> {
private T length;
private T width;
private T height; }
public Dimension(T length, T width, T height){
super();
this.length = length;
this.width = width;
this.height = height;
}
www.edureka.co/java-j2ee-soa-training
GENERIC FUNCTIONS
www.edureka.co/java-j2ee-soa-training
GENERIC FUNCTIONS
We can also write generic functions that can be
called with different types of arguments based
on the type of arguments passed to generic
method, & further the compiler handles each
method.
www.edureka.co/java-j2ee-soa-training
www.edureka.co/java-j2ee-soa-training
ADVANTAGES OF
GENERICS IN
JAVA
Code Reusability
Type Safety
Individual type casting not required
Implementing non generic algorithms
www.edureka.co/java-j2ee-soa-training
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
www.edureka.co/java-j2ee-soa-training

What are Generics in Java | Java Generics Tutorial | Edureka