Automatic type conversion in Java?
- by davr
Is there a way to do automatic implicit type conversion in Java? For example, say I have two types, 'FooSet' and 'BarSet' which both are representations of a Set. It is easy to convert between the types, such that I have written two utility methods:
/** Given a BarSet, returns a FooSet */
public FooSet barTOfoo(BarSet input) { /* ... */ }
/** Given a FooSet, returns a BarSet */
public BarSet fooTObar(FooSet input) { /* ... */ }
Now say there's a method like this that I want to call:
public void doSomething(FooSet data) {
/* .. */
}
But all I have is a BarSet myBarSet...it means extra typing, like:
doSomething(barTOfoo(myBarSet));
Is there a way to tell the compiler that certain types can automatically be cast to other types? I know this is possible in C++ with overloading, but I can't find a way in Java. I want to just be able to type:
doSomething(myBarSet);
And the compiler knows to automatically call barTOfoo()