We can create instances of the Party<T>
class by replacing the T
generic type parameter with any type name that adapts to the type constraints specified in the declaration of the Party<T>
class. So far, we have three concrete classes that implement both the Sociable
and Comparable<Sociable>
interfaces: SocialLion
, SocialParrot
, and SocialSwan
. Hence, we can use SocialLion
to create an instance of Party<SocialLion>
, that is, a Party
of SocialLion
. We take advantage of type inference and we use the previously explained diamond notation. This way, we will create a party of lions, and Simba
is the party leader. The code file for the sample is included in the java_9_oop_chapter_10_01
folder, in the example10_01.java
file.
SocialLion simba = new SocialLion("Simba", 10); SocialLion mufasa = new SocialLion("Mufasa", 5); SocialLion scar = new SocialLion("Scar", 9); SocialLion nala = new SocialLion("Nala", 7); Party<SocialLion...