Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method parameter must be obj of certain class that implements certain interface

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?

like image 465
Stan Avatar asked Feb 15 '13 16:02

Stan


1 Answers

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){} 
like image 90
assylias Avatar answered Oct 10 '22 01:10

assylias