MainMenu

Home Java Overview Maven Tutorials

Saturday, 12 October 2024

Generics in Java with Example




Hello Friends,

Generics , This concept, introduced in JDK 5.0

Generics in Java are a powerful feature that allows you to define classes, interfaces, and methods with type parameters. This means that you can write flexible and reusable code while maintaining type safety, as it helps prevent runtime type casting errors by catching them at compile time.


Advantages of Generics:

Type Safety: Compile-time type checks prevent runtime ClassCastException.
Code Reusability: You can write a generic class or method once and use it for different data types.
Elimination of Casts: When retrieving elements from a generic collection, there's no need to cast the result.




Generic Class Example:




import java.util.*;

class A<T>{
private T item;
public A(T item){
this.item=item;
}
public void set(T item){
this.item = item;
}
public T get(){
return item;
}
}

public class GenericDemo {
public static void main(String[] args){
ArrayList list = new ArrayList();
list.add(24);
// list.add("www.way2testing.com");
// list.add(new HashSet());
int x = list.get(0);
System.out.println(x);
A obj = new A("csc");
// obj.set("way2testing");
System.out.println(obj.get());
}
}

Output:


Generic Methods:

In Java, generic methods are methods that are written with a parameterized type, enabling them to operate on objects of various types while still being type-safe. The key feature of generics is that they allow you to write a method or class that can handle any data type, without sacrificing compile-time type checking.

Advantages of Generic Methods:

Type Safety: You get compile-time type checking and avoid ClassCastException. Code Reusability: A single generic method can be used for different data types, avoiding duplication.

Generic Methods Example:



public class GenericMethod {
public static <T> void gtest(T[] arr){
for(T a:arr){
System.out.println(a);
}
System.out.println("Generic Methods");
}

public static void main(String[] args){
Integer[] ia = {1,2,3,4,5};
gtest(ia);
String[] sa = {"csc", "way2testing", "datastop", "dbshop"};
gtest(sa);
}
}







Generic Interface:


In Java, a generic interface is an interface that can work with any type of data using type parameters. This allows the interface to be type-safe and reusable for different types, similar to generic classes and methods.



Generic Interface Example:



interface genricEngine<T>{
void set(T item);
T get();
}
class car implements genricEngine<String>{
private String accessories;
public void set(String accessories){
this.accessories = accessories;
}
@Override
public String get() {
return accessories;
}
}
class suv implements genricEngine<Integer>{
private Integer gear;
public void set(Integer gear){
this.gear = gear;
}
@Override
public Integer get() {
return gear;
}
}
public class InterfaceGeneric {
public static void main(String[] args){
car a = new car();
a.set("nitrogen");
System.out.println(a.get());
suv b = new suv();
b.set(5);
System.out.println(b.get());
}
}






No comments:

Post a Comment