What is Generic Class


Generic class is a class which has been developed for a generic type, and this class can be used with any specific type.This specific type need to be specified to instantiate the class.

Java Compiler generates code, based on the specific type used during instantiation.

Generic classes cannot be used with primitive data types.

Below is an example of Generic class. In this example Wrapper is Generic class.

//Generic Class
class Abc{

}

class Wrapper {

  private K m;

  public void put(K m) {
    this.m = m;
  }

  public K get() {
	  System.out.println("in get()");
    return m;
  }
}


public class Box
{
  public static void main(String[] args) {
     Wrapper integerBox = new Wrapper();
     Wrapper stringBox = new Wrapper();
     Wrapper abcBox = new Wrapper();

     //Generic class doesn't work for primitive data types
     //Wrapper iBox = new Wrapper();

     integerBox.put(new Integer(10));
     stringBox.put(new String("Hello Java652.com"));

     System.out.println("Integer Value is "+ integerBox.get());
     System.out.println("String Value is "+ stringBox.get());

     Abc tmp = abcBox.get();
     System.out.println("Abc value :"+tmp.toString());
  }
}


You may also like to read: Inheriting from Generic Class
Store objects in Generic HashSet

How to store objects in a HashSet Generic class

Below program shows how objects can be stored in any Collection. This program uses HashSet<T> instead of HashSet.

import java.util.HashSet;
import java.util.Iterator;

//Any objects can be stored in Set, or any other Collection
class Abc1
{
private int i,j;
Abc1(int i,int j)
{
this.i = i;
this.j = j;
}

public void display()
{
System.out.println(“Values are:”+i+” “+j);
}
}

public class SetObjectGeneric {
public static void main(String[] args) {

//Generic version of HasSet
HashSet<Abc1> hs = new HashSet<Abc1>();

//HashSet is internally implemented using hash table
Abc1 oa,ob,oc,od,oe;

oa = new Abc1(12,89);
ob = new Abc1(21,98);
oc = new Abc1(33,58);
od = new Abc1(68,89);
oe = new Abc1(32,26);

hs.add(oa);
hs.add(ob);
hs.add(oc);
hs.add(od);
hs.add(oe);

//System.out.println(” :”+hs);

Iterator itr = hs.iterator();
while(itr.hasNext())
{
Abc tmp = (Abc)itr.next();
tmp.display();
}

}

}

Also read our post How to store objects in a HashSet

How to store objects in a HashSet

Below program shows how objects can be stored in a Collection. Here all Objects stored in HashSet are of same type…in this example we are storing objects of class Abc, similarly any objects can be stored in a Collection.

import java.util.HashSet;
import java.util.Iterator;

//Any objects can be stored in Set, or any other Collection
class Abc
{
private int i,j;
Abc(int i,int j)
{
this.i = i;
this.j = j;
}

public void display()
{
System.out.println(“Values are:”+i+” “+j);
}
}

public class SetObjectDemo {
public static void main(String[] args) {

HashSet hs = new HashSet();

//HashSet is internally implemented using hash table
Abc oa,ob,oc,od,oe;

oa = new Abc(12,89);
ob = new Abc(21,98);
oc = new Abc(33,58);
od = new Abc(68,89);
oe = new Abc(32,26);

hs.add(oa);
hs.add(ob);
hs.add(oc);
hs.add(od);
hs.add(oe);

System.out.println(” :”+hs);

Iterator itr = hs.iterator();
while(itr.hasNext())
{
Abc tmp = (Abc)itr.next();
tmp.display();
}

}

}

You may also like How to store objects in a hashset generic class

Inheriting from Generic Class

Below is an example showing how to inherit from a Generic class

class Wrapper1<T> {

  private T m;

 public Wrapper1()
 {
	 System.out.println("Wrapper1() Constructor");
 }

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

  public T get() {
	  System.out.println("in get()");
    return m;
  }
}


class Sample extends Wrapper1<Float>
{
	public Sample()
	{
		System.out.println("Sample() Constructor");
	}

	public void met1()
	{
		System.out.println("met1() in Sample");
	}
}

class Example<K, L> extends Wrapper<L>
{
	private K n;

	public void add1(K p,L q)
	{
		add(q);
		n = p;
		System.out.println("add1()");
	}

	public void get1()
	{
		L h = get();

		System.out.println(n);
		System.out.println(h);
	}
}

public class GenericInherit
{
  public static void main(String[] args) {
     Wrapper1<Integer> integerBox = new Wrapper1<Integer>();
     Sample obj = new Sample();
     Example<Integer, String> objEx = new Example<Integer, String>();

     obj.met1();

     objEx.add1(new Integer(10), new String("Hello World"));
    objEx.get1();
  }
}