/Users/lyon/j4p/src/collections/getclass/Parent.java

1    /* 
2     * Parent.java 
3     * 
4     * Created on December 3, 2002 
5     */ 
6     
7    package collections.getclass; 
8     
9    /** 
10    * Base class using instanceof instead of getClass. 
11    * @author  Thomas Rowland 
12    */ 
13   public class Parent { 
14    
15       private static int id = 1; 
16    
17       public Parent() { 
18           System.out.println(toString()); 
19       } 
20    
21       /** 
22        * Overridden equals method using instanceof 
23        * instead of getClass causes problems when 
24        * comparing with derived classes. 
25        */ 
26       public boolean equals(Object o) { 
27           //if (o.getClass() == this.getClass()) 
28           if (o instanceof Parent) 
29               return (((Parent) o).getId() == this.id); 
30           return false; 
31       } 
32    
33       public String toString() { 
34           return "I am a Parent"; 
35       } 
36    
37       public int getId() { 
38           return this.id; 
39       } 
40    
41   } 
42