Generics

Generics can be thought of a pool containing as many objects as possible or allowing us to bring as many objects as we wish in i as well.

So the below code is a simple java bean holding two objects with getters and setters nothing has been specified because we don’t know what sort of object the consumer will be using an Integer or String. I named it as NonPair for a future purpose I’ll explain below

NonPair.java

public class NonPair {

	private Object a;
	private Object b;

	public NonPair(Object a, Object b) {
		super();
		this.a = a;
		this.b = b;

	}

	public Object getA() {
		return a;
	}

	public void setA(Object a) {
		this.a = a;
	}

	public Object getB() {
		return b;
	}

	public void setB(Object b) {
		this.b = b;
	}

}

Main Test class

public class TestMain {

	public static void main(String[] args) {
		NonPair nonPair = new NonPair("C++", new Integer(1983));
		String stringObj = (String) nonPair.getA();
		Integer integerObj = (Integer) nonPair.getB();

		System.out.println("The language " + nonPair.getA()
				+ " invented in " + nonPair.getB());
	}

}

Output

The language C++ invented in 1983

to explain the situation here is that we have a class which is acquiring a string and an integer values from the construction and values are set with String and Integer values, although we’ve defined just objects, keep in mind that even String and Integer classes are derived from Object class itself. So After values are set for each object, we naturally cast those values to acquire what has been filled with those values. So then we are able to grasp the values.

What if we don’t want the casting? there are two options for that we will either create such classes acquiring the values we want to for any purpose which will be tedious eventually in the end, or we will create classes which will acquire any object we want to regardless of casting. So the below the example is given;

Pair.java

public class Pair<A, B> {
	private A a;
	private B b;

	public Pair(A a, B b) {
		super();
		this.a = a;
		this.b = b;
	}

	public A getA() {
		return a;
	}

	public void setA(A a) {
		this.a = a;
	}

	public B getB() {
		return b;
	}

	public void setB(B b) {
		this.b = b;
	}

}

Main Test

public class TestMain {

	public static void main(String[] args) {
		Pair<String, Integer> pair = new Pair<> ("Java" , new Integer(1995));
		String stringObj = pair.getA();
		Integer integerObj = pair.getB();
		
		System.out.println("The language " + pair.getA()
				+ " invented in " + pair.getB());
	}

}

The outcome

The language Java invented in 1995

Here we see that we pass the objects as parameters to the regarding class and there is no need to identify what we wish to send as well as no need to the casting either.

Also there is a significant difference between 1.7+ and lower 1.7 is that you do need to identify value objects on the right side of the angled brackets in the initialization of the regarding class

jdk 1.7+ implementation

Pair<String, Integer> pair = new Pair<> ("Java" , new Integer(1995));

1.6 and lower implementation

Pair<String, Integer> pair = new Pair<String, Integer> ("Java" , new Integer(1995));

Leave a Reply

Your email address will not be published. Required fields are marked *