Whats
Whats

Reputation: 11

Naming methods to generic arguments

I am going over some code and I am adding some interfaces so i can make sure all the classes follow the same basic guidelines.
Now I have an interface called Uni < T > which takes exactly one generic argument.
Say I have a class which implements Uni< Foo > then I would like that class to have this method public foo getFoo(){};
Is there any way to add this method to the interface? I would prefer if i could do something like this in my interface public T getT(); and the second T gets replaced by the generic argument.

I can't just call it "get" because I would like some classes to implement multiple versions of Uni. public Example implements Uni<Foo1>,Uni<Foo2>

I know I can give the function 'get' an argument Foo or Class but I'd rather not so I thought I'd ask here. (I would also mess up the naming convection if i did this)

Upvotes: 0

Views: 47

Answers (2)

Priyamal
Priyamal

Reputation: 2969

its not possible with java but you can do it like this

public interface example<T> {
   public void yourmethod(T object);
}

make the interface according to the generic type you want.

Upvotes: 0

mtj
mtj

Reputation: 3554

No, not possible. And as generics in Java are implemented via erasure (i.e. dropped by the compiler), you will also not be able to implement multiple versions in a single class.

Upvotes: 1

Related Questions