i have 2 objects in following way
public class objecta { int id ; string name; } public class objectb { long id; string name; } i want able create interface 'anobject' implemented these 2 objects. how interface like?
public interface anobject { public <type?> getid() ; public string getname(); } what should type in getter id?
first of all, not name object. object java's implicit base class other classes. technically, name interface object long not place in package java.lang, highly misleading.
to provide different return types getid() in objecta , objectb, use generics:
public interface myobject<t> { t getid(); string getname(); } public class objecta implements myobject<integer> { @override public integer getid() { return 0; } @override public string getname() { return "a"; } } public class objectb implements myobject<long> { @override public long getid() { return 0; } @override public string getname() { return "b"; } } if getid() returns number, define myobject myobject<t extends number>. note cannot use native types int , long generics; have use boxed types integer , long.
Comments
Post a Comment