PostHeaderIcon Java Generics

Generics are a new feature of Java 1.5 and are generally used for validation purposes at compile time. For example, if you have an Array List of Person objects, then you can use Generics to ensure that every object that is added to the Array List is a Person and not another Object e.g. a Cat or a Dog Object. The example in this post is very simple as Generics can become more complicated after considering Inheritance.

//Person Class*****

public class Person implements PersonInterface, Comparable
{
private String id;
private String name;
private String dateOfBirth;
private int age;

public Person (String id2, String name2, String dateOfBirth2, int age2)
{
id = id2;
name = name2;
dateOfBirth = dateOfBirth2;
age = age2;

}

public String toString()
{
//return id +” “+name+” “+dateOfBirth+” “+age;
return name;
}

public int compareTo(Object o)
{
Person p = (Person)o;
return name.compareTo(p.getName());
}

public void setId(String id2)
{
id = id2;
}

public void setName(String name2)
{
name = name2;
}

public void setDateOfBirth(String dateOfBirth2)
{
dateOfBirth = dateOfBirth2;
}

public String getId()
{
return id;
}

public String getName()
{
return name;
}

public String getDateOfBirth()
{
return dateOfBirth;
}

public void setAge(int age2)
{
age = age2;
}

public int getAge()
{
return age;
}

public boolean equals(Object o)
{
if ((o instanceof Person) && (((Person)o).getAge() == this.age))
{
return true;
}
else
{
return false;
}
}

//*****

//Person Interface

public interface PersonInterface
{
public void setId(String id2);
public void setName(String name2);
public void setDateOfBirth(String dateOfBirth2);
public String getId();
public String getName();
public String getDateOfBirth();
public int compareTo (Object o);
}

//*****

//TestPerson*****

import java.util.*;
import java.util.ArrayList;
import java.util.List;
public class TestPerson
{
public static void main (String [] args)
{
Person p1 = new Person (“1″, “Joe Bloggs”, “04/11/1981″, 28);
//System.out.println(“Name before change = ” +p.getName());
//p.setName(“Ian F Stanford”);
//System.out.println(“Name after change = ” +p.getName());
Person p2 = new Person (“1″, “Jim Bloggs”, “04/11/1981″, 28);
Person p3 = new Person (“3″, “Janet Bloggs”, “05/01/1984″, 25);
Person p4 = new Person (“4″, “Andy Simmons”, “09/10/1985″, 24);
Person p5 = new Person (“5″, “Craig Harwood”, “09/10/1985″, 24);

List <Person>People = new ArrayList<Person>(); //these have to be Persons and not PersonInterfaces like with the legacy class I have written – warnings appear when compiling the legacy class.
People.add(p1);
People.add(p2);
People.add(p3);
People.add(p4);
People.add(p5);
System.out.println (“Before sort: ” +People);
Collections.sort(People);
System.out.println (“After sort: ” +People);
System.out.println (“Before call to PersonInterface.setName: ” +People);
PersonInterface pi = p1;
p1.setName(“Ian Stanford”);
System.out.println (“After call to PersonInterface.setName: ” +People);

//If .equals() was not overridden in Person, then references would be compared instead of comparing by Name attribute
if (p1.equals(p2))
{
System.out.println(“Persons 1 and 2 have the same name”);
}
else
{
System.out.println(“Persons 1 and 2 don’t have the same name”);
}

if (p1.equals(p5))
{
System.out.println(“Persons 1 and 5 have the same name”);
}
else
{
System.out.println(“Persons 1 and 5 don’t have the same name”);
}
}
}

Leave a Reply

You must be logged in to post a comment.

Adsense