How to declare a method with parameter which must be same time a certain class which implements certain interface without declaring a special class for this?
Assume i have declared class ImageX and also declared interface Tagging. So I need to create some other class or method which has to receive only instances of ImageX which implements Tagging interface. How to declare such method? something like
private void someMethod (ImageX<Tagging> obj){}
but this is not correct of course. Yes I could check the obj if it implements needed interface but I want that check in parameters. And what if it is not some method but a constructor...
am I asking about generics maybe?
You can make the method generic and only accept objects that are both an ImageX and a Tagging by using a type intersection:
private <T extends ImageX & Tagging> void someMethod (T obj){}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With