Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting an object to a generic interface

I have the following interface:

internal interface IRelativeTo<T> where T : IObject {     T getRelativeTo();     void setRelativeTo(T relativeTo); } 

and a bunch of classes that (should) implement it, such as:

public class AdminRateShift : IObject, IRelativeTo<AdminRateShift> {     AdminRateShift getRelativeTo();     void setRelativeTo(AdminRateShift shift); } 

I realise that these three are not the same:

IRelativeTo<> IRelativeTo<AdminRateShift> IRelativeTo<IObject> 

but nonetheless, I need a way to work with all the different classes like AdminRateShift (and FXRateShift, DetRateShift) that should all implement IRelativeTo. Let's say I have a function which returns AdminRateShift as an Object:

IRelativeTo<IObject> = getObjectThatImplementsRelativeTo(); // returns Object 

By programming against the interface, I can do what I need to, but I can't actually cast the Object to IRelativeTo so I can use it.

It's a trivial example, but I hope it will clarify what I am trying to do.

like image 376
robertkroll Avatar asked Oct 21 '08 15:10

robertkroll


People also ask

Can you cast an interface to an object?

If you have a concrete class, you can cast it to the interface. If you have an interface, it is possible to cast to the concrete class. Generally, you only want to go in the first direction. The reason being that you shouldn't know what the concrete class is when you have only a pointer to the interface.

What is casting to an interface?

A type cast—or simply a cast— is an explicit indication to convert a value from one data type to another compatible data type. A Java interface contains publicly defined constants and the headers of public methods that a class can define.

What is generic interface?

Generic Interfaces in Java are the interfaces that deal with abstract data types. Interface help in the independent manipulation of java collections from representation details. They are used to achieving multiple inheritance in java forming hierarchies. They differ from the java class.


1 Answers

If I understand the question, then the most common approach would be to declare a non-generic base-interface, i.e.

internal interface IRelativeTo {     object getRelativeTo(); // or maybe something else non-generic     void setRelativeTo(object relativeTo); } internal interface IRelativeTo<T> : IRelativeTo     where T : IObject {     new T getRelativeTo();     new void setRelativeTo(T relativeTo); } 

Another option is for you to code largely in generics... i.e. you have methods like

void DoSomething<T>() where T : IObject {     IRelativeTo<IObject> foo = // etc } 

If the IRelativeTo<T> is an argument to DoSomething(), then usually you don't need to specify the generic type argument yourself - the compiler will infer it - i.e.

DoSomething(foo); 

rather than

DoSomething<SomeType>(foo); 

There are benefits to both approaches.

like image 168
Marc Gravell Avatar answered Oct 06 '22 19:10

Marc Gravell