import java.util.* ;
class GenericDemo191b
{
public static void main(String[] args)
{
ArrayList<Person> al = new ArrayList<Person>();
al.add(new Person("abc1"));
al.add(new Person("abc2"));
al.add(new Person("abc4"));
ArrayList<Student> al1 = new ArrayList<Student>();
al1.add(new Person("a-1"));
al1.add(new Person("a-2"));
al1.add(new Person("a-4"));
print(al);
print(al1);
public static void print(ArrayList<? extends Person> al)
{
for (Iterator<? extends Person> it = al.iterator();it.hasNext() ; )
{
System.out.println(it.next().getName());
}
}
}
}
class Person
{
private String name ;
Person(String name )
{
this.name = name ;
}
public String getName()
{
return name ;
}
}
class Student extends Person
{
Student(String name )
{
super(name) ;
}
}
|
|