# Inheritance using Generics

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

![](/files/-Lf2oYyDKbvufa_is1mU)

![](/files/-Lf2oaTKBf6Og8gNTZqF)

{% 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)

![](/files/-Lf2wQ7TYTZGLoW8ZJ3z)

#### 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-core-java-for-java.gitbook.io/project/4-generics/inheritance-using-generics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
