What kind of type I define an argument that is of determinate class and implements determinate interface? [closed]
What type define an argument to make sure this is of determinate class and implements determinate interface? I'm using <T extends MyBaseClass & MyInterface> for argument type in method definition but my argument not extends MyBaseClass, IS of MyBaseClass type. I want to make sure that the argument that I recive are of MyBaseClass type and implements MyInterface
You can't. You either require the argument to be of a certain concrete type (and this means its subclasses are allowed too) or to implement a certain interface.
With generics you can force an argument to be parameterized with one or more types, such as subtypes are disallowed, but as far I can see it's not pertinent to your question.
You can however, use instanceof
in the body as well as doing comparison with arg.getClass()
.
I'm going to assume that you are asking if you can do something like this --
public <T extends YourBaseClass, YourInterface> void foo(T bar) {
The answer is yes, you can. If you try passing an argument into this method thats YourBaseClass, but does not implement YourInterface, then the code will not compile.
精彩评论