Generics in Java


Generics in java means parameterized types to deal with type-safe objects. It allows type like Integer, String, …etc., and user-defined types to be a parameter to methods, classes, and interfaces.

Class, method, or interface which operates on this parameterized type is called a Generic entity.

Note: Generics cannot work with primitive types.

The following example illustrates the use of generics wherein the Player class is an abstract class. FootballPlayer and BasketballPlayer are the classes that manage the players of the football team and basketball team respectively. Both of these classes extend Player class which contain property shared by both of the class. Class Team contains the functionality to manage the activities of all teams.

public abstract class Player {
private String name;

public Player(String name) { = name;

public String getName() {
return name;

public class FootballPlayer extends Player {
public FootballPlayer(String name) {


public class BaseballPlayer extends Player {
public BaseballPlayer(String name) {

Here Team class accepts the generic type as a parameter:

public class Team<T extends Player> {

private String name;
int played = 0;
int won = 0;
int lost = 0;
int tied = 0;

private ArrayList<T> members = new ArrayList<>();

public Team(String name) { = name;

public String getName() {
return name;

public boolean addPlayer(T player) {
if (members.contains(player)) {
//No need to typecast here as <T extends Player>.
System.out.println(player.getName() + " is already in this team");
return false;
} else {
System.out.println(player.getName()+ "Picked for this team");
return true;

public int numPlayers() {
return this.members.size();

public void matchResult(Team<T> opponent, int ourScore, int theirScore) {
String message;
if(ourScore > theirScore) {
message = " Beat ";
} else if (ourScore == theirScore) {
message = " Drew with ";
} else {
message = " Lost to ";
if (opponent != null) {
System.out.println(this.getName() + message + opponent.getName());
opponent.matchResult(null, theirScore, ourScore);

public int ranking() {
return (won * 2) + tied;

Team<T extends Player> implies Team class accepts parameter of type Player and the classes that extends Player class. In our case both FootballPlayer and BaseballPlayer.

Team<T> implies class accepts the generic parameter but we need to type cast to respective type when using the respective type. (main class)

public class Generics {
public static void main(String args[]) {

Team<FootballPlayer> starKins = new Team<>("StarKins");
FootballPlayer fb = new FootballPlayer("FB");

Team<FootballPlayer> wideWings = new Team<>("WideWings");
FootballPlayer ob = new FootballPlayer("OB");

Team<BaseballPlayer> baseBallTeam = new Team<>("Sunrisers");
BaseballPlayer bb = new BaseballPlayer("BB");
//Comparing Football teams "starKins" and "Sunrisers".
starKins.matchResult(wideWings, 2, 1);
System.out.println(starKins.getName() + " : " + starKins.ranking());
System.out.println(wideWings.getName() + " : " + wideWings.ranking());

  • We can specify multiple classes in the generic type declaration like this :
public class Team<T extends Player & Coach & Manager>

Why use Generics:

  1. Individual typecasting is not needed.
  2. It allows code reusability.
  3. It provides compile-time safety.

Complete code for this available at :




This publication contains articles from basics to latest features of Java

Recommended from Medium

Quick pro tips for Bitbucket pipeline

Building a music recognization app in SwiftUI with ShazamKit

Circuit Breaker with Microservices

Type System Savings

How we used customer feedback to launch our Slack integration in 20 days

How to upgrade a Rococo fork runtime

Unity Dashboard — lessons learned scaling our frontends, development culture and processes

Ownly Market Update: Version 4.0.0 Features

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shradha Yewale

Shradha Yewale

Software Development Engineer | API Development | Web Development| Java | Spring Boot | React JS

More from Medium

Annotations In Java

Java vs C++ Differences

Arranging Java Records

Why String is Immutable in Java?