# 4) Generics

| Intro                     |   |
| ------------------------- | - |
| Generic Class             |   |
| Generic Method            |   |
| Generic Class inheritance |   |
| Generic Interface         |   |
|                           |   |
|                           |   |

### Introduction :

Generics in Java is similar to templates in C++. The idea is to allow type (Integer, String, … etc and user defined types) to be a parameter to methods, classes and interfaces. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well. We can use them for any type.

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeyjlZmEGjovUyVNa41%2F-Leyk8zOfBl1zzZUdTyg%2FScreenshot%202019-05-16%20at%209.10.34%20AM.png?alt=media\&token=8eb88ab2-4ece-4ca9-a8e4-7e5fc257dfd8)

#### Code :

```java
package com.gns.generics;

class Parcel {
	private Object obj;

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}
}

class Phone {

}

class Book {

}

public class Generic1 {
	public static void main(String[] args) {
		Parcel parcel = new Parcel();
		parcel.setObj(new Book());
		System.out.println((Phone) parcel.getObj());
	}
}
```

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeyjlZmEGjovUyVNa41%2F-LeylMDeTElk1CFxoeA6%2FScreenshot%202019-05-16%20at%209.11.04%20AM.png?alt=media\&token=4431ddde-b03e-485f-b379-f6ed00dabebd)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeyjlZmEGjovUyVNa41%2F-LeylOxXLe1yFmpqyRRe%2FScreenshot%202019-05-16%20at%209.11.25%20AM.png?alt=media\&token=bced49b5-d55c-4c68-bc30-20758c932c66)

As shown in the preceding code, the declaration of generic class Parcel includes the type parameter T. After adding the type information, it’s read as Parcel or Parcel of T. The generic class Parcel defines a private instance variable of type T, and get() and set() methods to retrieve and set its value. Methods get() and set() use the parameter type T as their method parameter and return type.

{% hint style="info" %}
#### The first occurrence of T is different from its remaining occur- rences because only the first one is surrounded by <>.
{% endhint %}

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeyjlZmEGjovUyVNa41%2F-Leym5NBisKbWDNXAi9G%2FScreenshot%202019-05-16%20at%209.25.47%20AM.png?alt=media\&token=ba40561a-4339-4a4e-aa49-40a140a74d11)

With the generic class Parcel, UseGenericParcel can use method set() to assign an object of type Book. But UseGenericParcel can’t cast the retrieved object to an unrelated class, say, Phone. If it tries to do so, the code won’t compile (as shown in figure).

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeyjlZmEGjovUyVNa41%2F-LeymHV0aLzm_Q6GiXbE%2FScreenshot%202019-05-16%20at%209.26.26%20AM.png?alt=media\&token=c4950d8c-20f0-4593-8916-6d4af8cd2cc0)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeyjlZmEGjovUyVNa41%2F-Leyn0T6xfEz5EL-6KYh%2FScreenshot%202019-05-16%20at%209.29.32%20AM.png?alt=media\&token=93b3baaf-350f-40c7-9180-8a2206ff08b2)

#### Code :

```java
package com.gns.generics;

class Parcel<T> {
	private T obj;

	public T getObj() {
		return obj;
	}

	public void setObj(T obj) {
		this.obj = obj;
	}
}

class Phone {

}

class Book {

}

public class Generic2 {
	public static void main(String[] args) {
		Parcel<Book> parcel = new Parcel<Book>();
		parcel.setObj(new Book());
		System.out.println((Phone) parcel.getObj());
	}
}
```
