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

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();
  }
}

Inheriting from a 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();
  }
}

static block

static block gets executed before constructor is invoked.
static block is executed only once, whether one or more than one object is created.
As shown in below example more than one static block can exist in a class. They get executed in the order in which they are written.
Also non static block can exist in a class. A non static block gets executed once for every object, unlike static block.

static and non static block example:

class TestMe{
	{
		System.out.println("This is simple block");
	}

	static {
		System.out.println("in static block 1 TestMe");
	}

	static {
		System.out.println("in static block 2 TestMe");
	}

	TestMe()
	{
		System.out.println("in TestMe()");
	}
}

public class StaticBlockDemo {

	static{
		System.out.println("in static block StaticBlockDemo");
	}
	public static void main(String[] args) {

		System.out.println("in main program");

		//static block is executed before constructor is invoked
		//static block is executed only once, whether one or more than one object is created
		TestMe tm = new TestMe();
		TestMe tm1 = new TestMe();
		TestMe tm2 = new TestMe();
	}

}

Output:
in static block StaticBlockDemo
in main program
in static block 1 TestMe
in static block 2 TestMe
This is simple block
in TestMe()
This is simple block
in TestMe()
This is simple block
in TestMe()