How to give the And condition in Generics in java -
we have class structure a<-b<-c<-d<-e<-f
( b extends a, c extends b , on ).
i need create method acceptlist(list<> list)
in such way can accept list
of class a,c,e
, not accept list of b,d,f
.
this question asked me in interview.
some trick might work, bear in mind interfaces beat reflection ... if can implement type dependent functions in interfaces directly it.
first implementation of classes:
public class { public boolean isacceptable() { return true; } static class b extends { @override public boolean isacceptable() { return !super.isacceptable(); } } static class c extends b { @override public boolean isacceptable() { return !super.isacceptable(); } } static class d extends c { @override public boolean isacceptable() { return !super.isacceptable(); } } static class e extends d { @override public boolean isacceptable() { return !super.isacceptable(); } } static class f extends e { @override public boolean isacceptable() { return !super.isacceptable(); } }
}
then acceptlist(~):
public static boolean acceptlist(list<? extends a> list){ (a : list) { if(!a.isacceptable()){ return false; } } return true; }
Comments
Post a Comment