Generics and wild card concepts

Dan Dias Abeyesinghe
Javarevisited
Published in
4 min readMay 8, 2023

--

Photo by Boitumelo Phetla on Unsplash

Generics can be considered as a facility of generic programming. This was mainly designed to extend Java’s type system to allow a type or method to operate on objects of various types while providing compile-time type safety.

The word Generics means parameterized types. This means that it will allow the type (String, Integer, user-designed types… etc.) to be a parameter in interfaces, classes and methods. So, any interface, class or method can use any datatype (only reference types) with this concept of generics. In simple words it allows the user to decide the data type that can be used by a constructor of a class, and method which makes it possible dynamically. finally, we can conclude that it is possible to create classes that work with different data types using generics.

Generic Class

We can use <> to specify the parameter type when we are creating a generic class. However, it is important to note that we cannot use primitive data types like int, char or double. Look at the below code where we have created a Generic Class.

Output
50
Hello world

As we can see above, we have created a class called “GenericClass where the type of the class is set to” Type”, and we have declared an object of type “Type”. Then in the main class, we have created an instance of the class of Integer type first and then of String type and the output of those two can be seen respectively as the output.

Also, note that it is possible to have multiple Type parameters passed to the Generic Class. In simple words, we can have multiple parameters with different types passed into a class. Look at the below example code snippet.

Output
50
Hello World

As we can see in the above code snippet, we have declared 2 types of objects, one being of type “Type” and the other being of type “S”. We can see we can use multiple Type of parameters in the Generic class too.

Wild card concepts

The wild card concept follows the procedure of allowing an unknown type to be passed as a parameter instead of a type parameter used in generics.

The only limitation or restriction of the wild card concept is that it cannot be used as type argument as generics while invoking it.

Java has 3 types of Wild card concepts, those are,

· Upper bounded

· Lower bounded

· Unbounded

Upper bounded

This is similar to bounded types in generics. This method allows us to use all the subtypes of the defined class as a typed parameter.

This restricts the unknown type to a specific type or a subtype.

For example, if a certain function wants to accept the Number type and its subtypes of it, we can make it possible by just adding a “?” followed by the “extend” keyword and the class name.

Look at the code snippet below to understand it better.

In the above code snippet, a collection object is accepted as a parameter with the type of the parameter as a subclass of the Number class. However, if you pass any other type other than the subclass type of a Number you will get an error. (“passing a String would cause an error”).

Lower bounded

As the upper bounded allows to use of all subtypes of a class as a typed parameter, the same way lower bound restricts the unknown type to a particular type or supertype of the class.

To create a lower bounded card, we can do it by adding a “?” followed by a super keyword and the class name.

In the above code snippet, a collection object is accepted as earlier with the type of the parameter being of a superclass of the Integer class. Again, as before if a collection object with a different type parameter than the integer class and its supertype is passed, the code will generate a compile error.

Unbounded

This allows the usage of all the subtypes of an unknown type. Here any object type of parameter is accepted.

To use this, we simply add “?” as the typed parameter inside brackets.

As seen above if you want to accept an ArrayList of object type as a parameter we can use the unbounded method. However, if we pass a list object created from arrays and it contains primitive types, a compile-time error will be generated.

I hope this article on the brief introduction to Generics and wild card concepts helped you to get a better and clear understanding of the discussed topics. Please do feel free to provide feedback on the above and do follow for more articles related to this and many more!

--

--