# Inheritance using Generics

### GENERIC CLASS EXTENDING ANOTHER GENERIC CLASS :&#x20;

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf2oDDe7lamdiMtCm3k%2F-Lf2oYyDKbvufa_is1mU%2FScreenshot%202019-05-17%20at%208.54.28%20AM.png?alt=media\&token=5e6ced49-0619-4ec2-9929-cf7e7c50e135)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf2oDDe7lamdiMtCm3k%2F-Lf2oaTKBf6Og8gNTZqF%2FScreenshot%202019-05-17%20at%208.54.42%20AM.png?alt=media\&token=d69ab9d5-a603-450b-8d49-312fd8f521c8)

{% hint style="info" %}

#### A type argument must be passed to the type parameter of a base class. You can do so while extending the base class or while instantiating the derived class.

{% endhint %}

### Examples :

#### 1)

```java
package com.gns.generics;

class Parcel<T> {

}

class GenericParcel<T> extends Parcel<T> {

}

public class Generic3 {
	public static void main(String[] args) {
		Parcel<String> parcel = new GenericParcel<String>();
		// Parcel<String> parcel1 = new Parcel<Integer>(); won't compile
		// Parcel<String> parcel1 = new Parcel<Object>(); wont't compile
	}

}
```

#### 2)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf2vpGSPFJu8iVBr_5V%2F-Lf2wQ7TYTZGLoW8ZJ3z%2FScreenshot%202019-05-17%20at%209.28.14%20AM.png?alt=media\&token=f17d179d-9c10-43cf-944a-a1d7719ccf93)

#### 3)

```java
package com.gns.generics;

class Parcel<T> {

}

class GenericParcel<X,T> extends Parcel<T> {

}

public class Generic3 {
	public static void main(String[] args) {
		Parcel<String> parcel = new GenericParcel<Integer,String>();
		// Parcel<String> parcel1 = new Parcel<Integer>();
		// Parcel<String> parcel1 = new Parcel<Object>();
	}

}
```

#### 4)&#x20;

```java
package com.gns.generics;

class Book{
	
}
class Parcel<T> {

}

class GenericParcel<X> extends Parcel<Book> {

}

public class Generic3 {
	public static void main(String[] args) {
		Parcel<Book> parcel = new GenericParcel<Integer>();
		// Parcel<String> parcel1 = new Parcel<Integer>();
		// Parcel<String> parcel1 = new Parcel<Object>();
	}

}
```

#### 5)

```java
package com.gns.generics;

class Parcel<T> {

}

class GenericParcel<X> extends Parcel<String> {

}

public class Generic3 {
	public static void main(String[] args) {
		Parcel<String> parcel = new GenericParcel<Integer>();
	}

}
```

### NON-GENERIC CLASS EXTENDING A GENERIC CLASS

You can extend a generic base class to define a nongeneric base class. To do so, the derived class doesn’t define any type parameters but passes arguments to all type parameters of its generic base class. For example

```java
package com.gs.corejava.generics;

class Phone{
	
}
class Parcel<T>{
	
}
class NonGenericPhoneParcel extends Parcel<Phone> {
	
}
public class Generic4 {

}
```

In the preceding example, NonGenericPhoneParcel is a nongeneric class that passes argument Phone to its base class Parcel.

{% hint style="info" %}

#### You can’t pass type arguments to a nongeneric class.

{% endhint %}
