Parameterized Generic shape in addition to method inwards Java
Writing Generic parametrized shape in addition to method inwards Java is slowly in addition to should live on used equally much equally possible. Generic inwards Java was introduced inwards version 1.5 along amongst Autoboxing, Enum, varargs in addition to static import. Most of the novel code evolution inwards Java uses type-safe Generic collection i.e. HashSet inwards house of HashSet but notwithstanding Generic is underused inwards price of writing ain parametrized classes in addition to method. I grip that most Java programmers has started using Generic land working amongst the Java collection framework but they are notwithstanding non certain how Generic tin allow y'all to write Template form of classes which tin move amongst whatever Type only similar the parametrized ArrayList inwards Java which tin shop whatever Type of element. In the final distich of article about Generics nosotros induce got seen How Generic plant inwards Java in addition to explored wild cards of Generic inwards Java in addition to In this business office of Java Generic illustration nosotros volition run across How to write parametrized Generic Class in addition to method inwards Java.
Hashtable inwards Java. This generic shape volition incorporate 2 parametrized method T getItem () in addition to setItem(T) whose Type volition live on determined at the fourth dimension of instantiation. We volition besides run across the quondam version of the same shape which is written without using Generic to demonstrate concrete exercise goodness offered past times Generic type-safety inwards price of coding in addition to development.
Guideline of writing parameterized Generic class:
1) Use type parameter in Class annunciation e.g. shape Wrapper where T is a Generic type parameter stands for Type, y'all tin besides role which stands for Element in addition to much suitable for collection form of information construction which stores elements.
2) Now role this T inwards all places where y'all bespeak to role actual Type e.g. While declaring the method argument, land writing render type of method etc.
/**
* Java plan to demonstrate How to write parameterized shape inwards Java in addition to type-safety
* provided past times parameterized class. Program besides compares non parameterized to
* Java plan to demonstrate How to write parameterized shape inwards Java in addition to type-safety
* provided past times parameterized class. Program besides compares non parameterized to
* parameterized shape to highlight number amongst non generic classes inwards Java.
*
* @author Javin Paul
*/
public class GenericTest {
public static void main(String args[]) {
*
* @author Javin Paul
*/
public class GenericTest {
public static void main(String args[]) {
//string wrapper
Wrapper<String> stringWrapper = new Wrapper<String>();
stringWrapper.setItem("Test");
System.out.println(stringWrapper.getItem());
//compilation error, type checking at compile time
//stringWrapper.setItem(new StringBuffer(""));
Wrapper<Integer> integerWrapper = new Wrapper<Integer>();
integerWrapper.setItem(123);
//compilation error, type security checking
//integerWrapper.setItem("123");
System.out.println(integerWrapper.getItem());
// Now let's run across how to write generic wrapper without using
// JDK1.5 generic in addition to what employment it poses
OldWrapper oldStringWrapper = new OldWrapper();
//no compilation mistake i.e. no type checking at compile time
oldStringWrapper.setItem(123);
//will throw ClassCastException at runtime
((String)oldStringWrapper.getItem()).toUpperCase();
}
}
/*
* wrapper tin wrap whatever item
* Generic parameterized shape of Wrapper, offers compile fourth dimension type checking
*/
class Wrapper<T> {
private T item;
public T getItem(){
return item;
}
public void setItem(T item){
this.item = item;
}
}
/*
* Object shape of Wrapper delicate in addition to mistake prone
*/
class OldWrapper{
private Object item;
public Object getItem(){
return item;
}
public void setItem(Object item){
this.item = item;
}
}
Wrapper<String> stringWrapper = new Wrapper<String>();
stringWrapper.setItem("Test");
System.out.println(stringWrapper.getItem());
//compilation error, type checking at compile time
//stringWrapper.setItem(new StringBuffer(""));
Wrapper<Integer> integerWrapper = new Wrapper<Integer>();
integerWrapper.setItem(123);
//compilation error, type security checking
//integerWrapper.setItem("123");
System.out.println(integerWrapper.getItem());
// Now let's run across how to write generic wrapper without using
// JDK1.5 generic in addition to what employment it poses
OldWrapper oldStringWrapper = new OldWrapper();
//no compilation mistake i.e. no type checking at compile time
oldStringWrapper.setItem(123);
//will throw ClassCastException at runtime
((String)oldStringWrapper.getItem()).toUpperCase();
}
}
/*
* wrapper tin wrap whatever item
* Generic parameterized shape of Wrapper, offers compile fourth dimension type checking
*/
class Wrapper<T> {
private T item;
public T getItem(){
return item;
}
public void setItem(T item){
this.item = item;
}
}
/*
* Object shape of Wrapper delicate in addition to mistake prone
*/
class OldWrapper{
private Object item;
public Object getItem(){
return item;
}
public void setItem(Object item){
this.item = item;
}
}
If y'all expect at higher upwardly illustration in addition to compare both parameterized versions of shape in addition to non parameterized or raw version of same class, You tin derive 2 substantial benefits of using generic parameterized shape in addition to method :
1) Parameterized classes offering compile fourth dimension type verification. Which is non nowadays inwards non Generic or non parametrized version of the class.
2) If y'all role Generic parametrized method or shape y'all don't bespeak to cast into a specific type
3) Generic methods don't throw ClassCastException equally Type verification was already done at compile time.
How to write parameterized method inwards Java Generics:
What is parameterized method inwards Java? is a popular Java Generics interview question in addition to followed past times inquiry similar how to write parameterized method using Generics. Parameterized method inwards Java are those method writing using Generics characteristic of Java which convey method declaration equally type and/or render type instead of whatever item type similar String, Double or Float. Parameterized method prevents duplication of code in addition to provides type-safety at compile time. Any static utility method which operate on Object type is expert candidate of making parameterized or Generic method inwards Java. Here is a code illustration of How to write Generic Parameterized method inwards Java:
/**
* Java plan to demonstrate how to write parameterized method inwards Java
* parameterized method needs a type parameter annunciation earlier return
* type, hither is a type parameter
* @author Javin
*/
class ItemUtils{
public static <T> T wrap(T item){
//code for wrapping item
return item;
}
}
* Java plan to demonstrate how to write parameterized method inwards Java
* parameterized method needs a type parameter annunciation earlier return
* type, hither
* @author Javin
*/
class ItemUtils{
public static <T> T wrap(T item){
//code for wrapping item
return item;
}
}
In Summary role Generic parameterized version of shape in addition to method inwards house of using Object equally generic type. If y'all induce got a code which is on Java five in addition to doesn’t role Generic, consider refactoring that code to role Generic to relish benefits provided past times Generics similar type-safety, no ClassCastException in addition to no bespeak of casting.
That's all on this Java Generic Example of how to exercise parameterized shape in addition to method using Generic. Just cry upwardly that Generic is entirely available from Java 1.5 onwards in addition to y'all tin non write parameterized shape in addition to method inwards Java 1.4 or lower version.
Further Learning
Introduction to Collections & Generics inwards Java
What is fork bring together framework inwards Java 7