I want a method that processes a list of any type that implements a particular interface.
public interface Encryptable {
public void encrypt() throws Exception ;
}
class DomainClass implements Encryptable {
String name ;
public void encrypt() throws Exception {
try {
name = CryptoUtils.encrypt(name);
}
}
}
This utility method can encrypt a List of any domain class that implements Encryptable.
public static void encryptList ( Collection<? extends Encryptable> listToEncrypt ) {
for ( Encryptable objToEncrypt: listToEncrypt ) {
try {
objToEncrypt.encrypt() ;
} catch (Exception e) {
}
}
}
I wrote a test app and this appears to work. What concerns me is the Java keyword 'extends'. My classes do not extend Encryptable they implement it. Does what I've written really work or am I the victim of some sort of side effect of doing things wrong but getting the right answer.