0

I want to write a method that gets 2 parameters:

  1. an interface.
  2. a class that implements that interface.

I want it to be generic (compile time type safe).

is there a way? if not, whats the alternative?

is there an option to get generic param that is an interface? ho do I declare it? ?

1
  • 6
    Post your code. Commented Nov 27, 2012 at 10:45

2 Answers 2

5

some thing like this ??

public <I, K extends I> void method(I i, K k){

    }

In the above method first parameter would be an interface, and second parameter would be any class that implements that interface.

    Interface1 i1;
    method(i1, class1); //class1 implements Interface1

In generics, interface implementation and class extending is represented using extends keyword. there is no implements keyword in the world of generics.

Sign up to request clarification or add additional context in comments.

1 Comment

looks good.. just extends is for classes , can i use implemtes?
2
public <T extends Interface> T myMethod(Interface I, Class<T> myClass) () {...}

if your method returns T

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.