Java Reference
In-Depth Information
Display 14.11
A Derived Generic Class
(part 2 of 2)
26 (getFirst().equals(otherPair.getSecond())
27 && getSecond().equals(otherPair.getFirst()));
28 }
29 }
30 }
Suppose
HourlyEmployee
is a derived class of the class
Employee
. You might
think that an object of type
Pair<HourlyEmployee>
is also of type
Pair<Employee>
.
You might think that, but you would be wrong. If
G
is a generic class, there is no
relationship between
G<A>
and
G<B>
, no matter what the relationship is between the
classes
A
and
B
.
Display 14.12
Using
UnorderedPair
1
public class
UnorderedPairDemo
2 {
3
public static void
main(String[] args)
4 {
5 UnorderedPair<String> p1 =
6
new
UnorderedPair<String>("peanuts", "beer");
7 UnorderedPair<String> p2 =
8
new
UnorderedPair<String>("beer", "peanuts");
9
if
(p1.equals(p2))
10 {
11 System.out.println(p1.getFirst() + " and " +
12 p1.getSecond() + " is the same as");
13 System.out.println(p2.getFirst() + " and "
14 + p2.getSecond());
15 }
16 }
17 }
Sample Dialogue
2
peanuts and beer is the same as
beer and peanuts
2
A note to the grammar police: I intentionally used “is” instead of “are.” If you read and understand
the text, you will realize that “peanuts and beer” is a single item. Starting the sentence with a lowercase
letter and the absence of a period are also intentional.