In: Computer Science
Generic types
A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic.
Open GenericsC.java in jGRASP then compile it.
At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection.
Since the List being declared (al) is a field of the GenericsC class, we will want to make the class itself generic in order to achieve the generality that we want.
Declare a type variable for the class.
public class GenericsC<T>
Use this type variable as the value for the type parameter of the List field al.
private List<T> al;
Compile GenericsC.java and notice the results.
One warning is the same as before (unchecked call to add for the raw type Collection), but the other warning has changed. Worse, though, we now have a type error, which means our code isn’t just unsafe: It’s incorrect.
Let’s try to correct the new warning first (unchecked conversion of ArrayList). Since ArrayList is a generic type, we can try to provide a type value for its type variable.
Use the type variable that we declared for this class as the value of the type parameter for ArrayList1. Your code should appear as follows:
al = new ArrayList<T>();
Compile GenericsC.java and notice the results.
Read the error message carefully and try to understand why al.add(o) is a problem.
javac -Xlint:unchecked GenericsC.java GenericsC.java:32: error: incompatible types: Object cannot be converted to T al.add(o); ^ where T is a type-variable: T extends Object declared in class GenericsC GenericsC.java:51: warning: [unchecked] unchecked call to add(E) as a member of the raw type Collection c.add(i); ^ where E is a type-variable: E extends Object declared in interface Collection Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error 1 warning
Fix this error by (1) using the parameterized type Collection<T> instead of the raw type Collection and (2) declaring o to be of type T instead of Object. Your code should appear as follows.
public void addAll(Collection<T> c) { for (T o : c) { al.add(o); } }
Compile GenericsC.java and confirm that this change eliminated the error.
Change the declaration of c to be of an appropriate parameterized type. Your code should appear as follows:
Collection<Integer> c = new ArrayList<Integer>();
Change the declaration of lab to be of an appropriate parameterized type. Your code should appear as follows:
GenericsC<Integer> lab = new GenericsC<Integer>();
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* GenericsC.java
* Used to illustrate basic principles of generic types
* and type safety in Java.
*/
////////////////////////////////////////////////
//
// Add appropriate type parameters and arguments
// to eliminate all unchecked warnings. That is,
// make this code type safe.
//
///////////////////////////////////////////////
public class GenericsC {
private List al;
/** Builds a new instance of this class. */
public GenericsC() {
al = new ArrayList();
}
/** Adds all the values in c to this object. */
public void addAll(Collection c) {
for (Object o : c) {
al.add(o);
}
}
/** Returns a string representation of this object. */
public String toString() {
StringBuilder s = new StringBuilder();
Iterator itr = al.iterator();
while (itr.hasNext()) {
s.append(itr.next());
s.append(" ");
}
return s.toString();
}
/** Drives execution. */
public static void main(String[] args) {
Collection c = new ArrayList();
for (int i = 1; i < 12; i += 2) {
c.add(i);
}
GenericsC lab = new GenericsC();
lab.addAll(c);
System.out.println(lab.toString());
}
}
Please code in java
After making the above changes, now the code looks as below.
/**
* GenericsC.java
* Used to illustrate basic principles of generic types
* and type safety in Java.
*/
////////////////////////////////////////////////
//
// Add appropriate type parameters and arguments
// to eliminate all unchecked warnings. That is,
// make this code type-safe.
//
///////////////////////////////////////////////
import java.util.*;
public class GenericsC<T>{
private List<T> al= new ArrayList<T>();
/** Builds a new instance of this class. */
public GenericsC() {
al = new ArrayList();
}
/** Adds all the values in c to this object. */
public void addAll(Collection<T> c) {
for (T o : c) {
al.add(o);
}
}
/** Returns a string representation of this object. */
public String toString() {
StringBuilder s = new StringBuilder();
Iterator itr = al.iterator();
while (itr.hasNext()) {
s.append(itr.next());
s.append(" ");
}
return s.toString();
}
/** Drives execution. */
public static void main(String[] args) {
Collection<Integer> c = new ArrayList<Integer>();
for (int i = 1; i < 12; i += 2) {
c.add(i);
}
GenericsC<Integer> lab = new GenericsC<Integer>();
lab.addAll(c);
System.out.println(lab.toString());
}
}
This code is completely free from bugs.
Thank you! if you have any queries post it below in the comment section I will try my best to resolve your queries and I will add it to my answer if required. Please give upvote if you like it.