Restrictions while declaring a generic (type) in Java

Java 8Object Oriented ProgrammingProgramming

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.

Restrictions on generics

You cannot use generics in certain ways and in certain scenarios as listed below −

  • You cannot use primitive datatypes with generics.
class Student<T>{
   T age;
   Student(T age){
      this.age = age;
   }
}
public class GenericsExample {
   public static void main(String args[]) {
      Student<Float> std1 = new Student<Float>(25.5f);
      Student<String> std2 = new Student<String>("25");
      Student<int> std3 = new Student<int>(25);
   }
}

Compile time error

GenericsExample.java:11: error: unexpected type
      Student<int> std3 = new Student<int>(25);
               ^
   required: reference
   found: int
GenericsExample.java:11: error: unexpected type
      Student<int> std3 = new Student<int>(25);
                                       ^
required: reference
found: int
2 errors
  • You cannot instantiate the generic parameters.
class Student<T>{
   T age;
   Student(T age){
      this.age = age;
   }
   public void display() {
      System.out.println("Value of age: "+this.age);
   }
}
public class GenericsExample {
   public static void main(String args[]) {
      Student<Float> std1 = new Student<Float>(25.5f);
      std1.display();
      T obj = new T();
   }
}

Compile time error

GenericsExample.java:15: error: cannot find symbol
      T obj = new T();
      ^
   symbol: class T
   location: class GenericsExample
GenericsExample.java:15: error: cannot find symbol
      T obj = new T();
                  ^
   symbol: class T
   location: class GenericsExample
2 errors
  • The generic type parameter cannot be static.
class Student<T>{
   static T age;
   Student(T age){
      this.age = age;
   }
   public void display() {
      System.out.println("Value of age: "+this.age);
   }
}
public class GenericsExample {
   public static void main(String args[]) {
      Student<Float> std1 = new Student<Float>(25.5f);
      std1.display();
   }
}

Compile time error

GenericsExample.java:3: error: non-static type variable T cannot be referenced from a static context
   static T age;
         ^
1 error
  • You cannot cast parameterized type of one datatype to other.
import java.util.ArrayList;
public class Generics {
   public static void main(String args[]) {
      ArrayList<Integer> list1 = new ArrayList<Integer>();
      list1.add(25);
      list1.add(26);
      ArrayList<Number> list2 = list1;
   }
}

Compile time error:

Generics.java:8: error: incompatible types: ArrayList<Integer> cannot be converted to ArrayList<Number>
   ArrayList<Number> list2 = list1;
            ^
1 error
  • You cannot create an array of generic type objects.
class Student<T>{
   T age;
   Student(T age){
      this.age = age;
   }
   public void display() {
      System.out.println("Value of age: "+this.age);
   }
}
public class GenericsExample {
   public static void main(String args[]) {
      Student<Float>[] std1 = new Student<Float>[5];
   }
}

Output

GenericsExample.java:12: error: generic array creation
   Student<Float>[] std1 = new Student<Float>[5];
                           ^
1 error
  • A generic type class cannot extend the throwable class therefore, you cannot catch or throw these objects.

class Student<T>extends Throwable{
   T age;
   Student(T age){
      this.age = age;
   }
   public void display() {
      System.out.println("Value of age: "+this.age);
   }
}
public class GenericsExample {
   public static void main(String args[]) {
   }
}

Compile time error

GenericsExample.java:1: error: a generic class may not extend java.lang.Throwable
class Student<T>extends Throwable{
                        ^
1 error
raja
Published on 06-Sep-2019 11:47:43
Advertisements