Java Reference
In-Depth Information
Display 15.15
A
PubliclyCloneable
Class
(part 2 of 2)
19
public
void
setName(String newName)
20
{
21
name = newName;
22
}
23
public
String toString( )
24
{
25
return
(name + " " + number);
26
}
27
public
Object clone( )
28
{
29
try
30
{
31
return
super
.clone( );
32
}
33
catch
(CloneNotSupportedException e)
34
{
//This should not happen.
35
return
null
;
//To keep compiler happy.
36
}
37
}
38
39
public
boolean
equals(Object otherObject)
40
{
41
if
(otherObject ==
null
)
42
return
false
;
43
else
if
(getClass( ) != otherObject.getClass( ))
44
return
false
;
45
else
46
{
47
StockItem otherItem = (StockItem) otherObject;
48
return
(name.equalsIgnoreCase(otherItem.name)
49
&& number == otherItem.number);
50
}
51
}
52
}