java - Overloading methods in an interface using generics as parameters -
i have java interface defining method takes interface b parameter. want know if possible overload method in implementing class give parameter object implements interface b. here's sample code of mean:
public interface sampleinterfacea{ public void samplemethod(sampleinterfaceb b); } public class classb implements sampleinterfaceb{} public class classc implements sampleinterfaceb{} public class sampleclassa implements sampleinterfacea{ @override public void samplemethod(classb b){} } public class sampleclassb implements sampleinterfacea{ @override public void samplemethod(classc c){} }
thanks in advance help.
if try it, you'll see sampleclassa
, sampleclassb
seen compiler missing needed implement sampleinterfacea
.
you need implement actual method defined in interface.
a way approximately seem want use generics:
public interface sampleinterfacea <t extends sampleinterfaceb>{ public void samplemethod(t b); } public class sampleclassa implements sampleinterfacea<classb>{ public void samplemethod(classb b){} } public class sampleclassb implements sampleinterfacea<classc>{ public void samplemethod(classc c){} }
Comments
Post a Comment