Skip to content

Instantly share code, notes, and snippets.

@fatso83
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatso83/9537928 to your computer and use it in GitHub Desktop.
Save fatso83/9537928 to your computer and use it in GitHub Desktop.
Example of using parametrised multiple bounds in Java 7.This is just showing how stuff is connected - it really does not make any sense in itself.Compiles with JDK7.
/*
Example of using parametrised multiple bounds in Java 7.
This is just showing how stuff is connected - it really does not make any sense in itself.
Compiles with JDK7.
*/
import java.util.ArrayList;
import java.util.List;
// parametrised interfaces
interface IF1<T>{T returnIt();}
interface IF2<T>{void doIt(T e);}
// un-parametrised multiple bounds using the above interfaces
abstract class AbstractClass1<T extends IF1 & IF2> {
List<T> aList;
void add(T e) {
aList.add(e);
}
@SuppressWarnings("unchecked")
void loopExecute(Object o) {
for(T e : aList) e.doIt(o);
}
@SuppressWarnings("unchecked")
Object pop() {
Object o = aList.remove(aList.size()-1);
return ((T) o).returnIt();
}
}
// parametrised multiple bounds
abstract class AbstractClass2<A,B,T extends IF1<A> & IF2<B>> {
List<T> aList;
void add(T e) {
aList.add(e);
}
void loopExecute(B o) {
for(T e : aList) e.doIt(o);
}
A pop() {
T o = aList.remove(aList.size()-1);
return o.returnIt();
}
}
// concrete implementation
// inheritence of parametrised multiple bounded class
class ConcreteClass <A,B,T extends IF1<A> & IF2<B>> extends AbstractClass2<A,B,T>{
ConcreteClass() { aList = new ArrayList<>(); }
void add(T e) {
aList.add(e);
}
void loopExecute(B o) {
for(T e : aList) e.doIt(o);
}
A pop() {
T o = aList.remove(aList.size()-1);
return o.returnIt();
}
}
public class GenericsDemo {
public static void main(String[] arg) {
class Dummy<T,U> implements IF1<T>,IF2<U> {
T e;
Dummy(T e) { this.e = e;}
@Override
public T returnIt() { return e; }
@Override
public void doIt(U param) { System.out.println(e + "DO IT ! " + param);}
}
class Dummy2<T,U> extends Dummy<T,U> { Dummy2(T e) { super(e); }}
ConcreteClass<String,Integer,Dummy<String,Integer>> c = new ConcreteClass<>();
c.add(new Dummy<String,Integer>("Jalla"));
c.add(new Dummy2<String,Integer>("Haram!"));
c.loopExecute(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment