Skip to content

Generic Methods And Classes In Java Programming Language

In this article, you will learn Generic Methods and Classes in Java Programming Language.

Generics are the software entities that reduce the duplication of source code by permitting a programmer to write common data types or methods for objects having different data types. Learn Java with help of professional instructor, Join Java Classes in Gurgaon and learn by working on live projects.

Generics were incorporated in Java programming language since version J2SE 5.0. They were created to leverage the type system and to provide type safety at the compile-time.

CLASSIFICATIONS OF GENERICS IN JAVA

Type Variable- It is an unreserved identifier, which means it doesn’t specify a particular type and serves as an expression that holds the place for a type that is brought into use when an ArrayList is used.

Some of the generally used Type Variables are shown below.

  1. T- Type
  2. K- Key
  3. E- Element
  4. V- Value
  5. N- Number

Class- A generic class has one or more Type Variables which are particularly known as the Type Parameters of the Class.

Interface- Like a generic class, a generic interface has one or more Type Variables which are particularly known as Type Parameters of the Interface.

Method- A generic method has one or more type variables which are known as Formal Type Parameters of the method. The formal type parameter list of the generic method is quite similar to that of a generic class or interface.

Constructor-  A generic constructor has one or more type variables which are known as the formal type parameters of the constructor. The formal type parameter list of the generic constructor is quite similar to that of a generic class or interface.

GENERIC CLASS IN JAVA

A class that can have one or more type parameters is known as generic class. If a generic class have more than one parameter separated by commas, it is known as parameterized class having parameterized types.

Generic Class enables

Syntax: 

class classname<type>

Example:

class GenericEx<T> {
T obj ;

public void add(T obj) {
this.obj = obj ;
}

public T get() {
return obj ;
}

public static void main(String[] args) {
GenericEx<Integer> gen1 = new GenericEx<Integer>() ;
GenericEx<String> gen2 = new GenericEx<String>() ;

gen1.add(new Integer(100)) ;
gen2.add(new String(“This is an example of Generic Class”)) ;

System.out.printf(“Value1(Integer) : %d\n”, gen1.get()) ;
System.out.printf(“Value2(String) : %s\n”, gen2.get()) ;
}
}

Output:

Value1(Integer) : 100
Value2(String) : This is an example of Generic Class

GENERIC METHOD IN JAVA

A method that can have one or more type parameters is known as generic method. We have shown below an example of generic method.

Example:

class GenericMethod {
public static <E> void displayArr(E [] inputarr) {
for ( E e : inputarr) {
System.out.print(e) ;
}
System.out.println() ;
}

public static void main( String args[] ) {
Integer[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ;
Character[] arr2 = {‘W’, ‘3’, ‘T’, ‘R’, ‘A’, ‘I’, ‘N’, ‘I’, ‘N’, ‘G’, ‘ ‘, ‘S’, ‘C’, ‘H’, ‘O’, ‘O’, ‘L’} ;

System.out.println(“Array1(Integer)”) ;
displayArr(arr1) ;

System.out.println(“Array2(Character)”) ;
displayArr(arr2) ;
}
}

Output:

Array1(Integer)
12345678910
Array2(Character)
W3TRAINING SCHOOL

ADVANTAGES OF GENERICS IN JAVA

Following are some advantages of using generics over non-generic classes and methods.

  • Provides type-safety at compile-time.
  • Generic codes do not require type-casting.
  • Using generic algorithms, it is easy to customize and read the codes.
  • Reduces code duplication by employing single method declaration and single class declaration.

We have provided you the description on Generic Methods and Classes in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest