edu.pdx.cs.joy.lang.GenericsAndLegacy2 Maven / Gradle / Ivy
The newest version!
package edu.pdx.cs.joy.lang;
import java.util.*;
/**
* This class demonstrates how updated domain classes that use
* generics can interact with legacy code that doesn't use generics.
*
* @author David Whitlock
* @since Winter 2005
*/
public class GenericsAndLegacy2 {
/**
* An updated domain class that uses generics
*/
static class Course {
private List allStudents =
new ArrayList();
void addStudents(Collection students) {
for (Student s : students) {
this.allStudents.add(s);
}
}
List getStudents() {
return this.allStudents;
}
}
/**
* A student
*/
static class Student {
}
/**
* A grad student
*/
static class GradStudent extends Student {
}
/**
* A "legacy" main program that interacts with an updated domain
* class that uses generic collections.
*/
public static void main(String[] args) {
Course course = new Course();
Collection students = new ArrayList();
students.add(new Student());
students.add(new GradStudent());
course.addStudents(students); // warning
Student student1 = course.getStudents().get(0);
}
}