001package edu.pdx.cs410J.j2se15; 002 003import java.util.*; 004 005/** 006 * This class demonstrates how updated domain classes that use 007 * generics can interact with legacy code that doesn't use generics. 008 * 009 * @author David Whitlock 010 * @since Winter 2005 011 */ 012public class GenericsAndLegacy2 { 013 014 /** 015 * An updated domain class that uses generics 016 */ 017 static class Course { 018 private List<Student> allStudents = 019 new ArrayList<Student>(); 020 021 void addStudents(Collection<Student> students) { 022 for (Student s : students) { 023 this.allStudents.add(s); 024 } 025 } 026 027 List<Student> getStudents() { 028 return this.allStudents; 029 } 030 } 031 032 /** 033 * A student 034 */ 035 static class Student { 036 037 } 038 039 /** 040 * A grad student 041 */ 042 static class GradStudent extends Student { 043 044 } 045 046 /** 047 * A "legacy" main program that interacts with an updated domain 048 * class that uses generic collections. 049 */ 050 public static void main(String[] args) { 051 Course course = new Course(); 052 Collection students = new ArrayList(); 053 students.add(new Student()); 054 students.add(new GradStudent()); 055 course.addStudents(students); // warning 056 057 Student student1 = course.getStudents().get(0); 058 } 059 060}