I’m creating several different classes, each to interact with a different API. I made an interface for each class to inherit from, but one of the methods in the interface can take in several different data types depending on which API is being implemented. Is it possible to define an unknown type for that method to take in?
For now I’m just declaring it as an Object and casting it within the implementation.
Interface:
public interface UDSession {
public void authenticate(String user) throws UDException;
public void linkAccount(Object authInfo) throws UDException;
}
Method implementations (just the signature and first line):
Dropbox:
public void linkAccount(Object authInfo) throws UDException {
WebAuthSession.WebAuthInfo info = (WebAuthSession.WebAuthInfo) authInfo;
}
Google:
public void linkAccount(Object authInfo) throws UDException {
GoogleOAuthParameters info = (GoogleOAuthParameters) authInfo;
}
I also tried defining it in the interface as: Class<?> and defining it in the method signature implementation as: Class<WebAuthSession.WebAuthInfo>. But this didn’t work.
There’s got to be a better way to do this than using Object type parameters. Any ideas?
Thanks.
Depending on how you instantiate your class you could try: