|
Prev: Java multiplication of numbers and
Next: (www.stefsclothes.com)supply jerseys,hangbag, caps,sunglasses etc (paypal accept)
From: Jason Cavett on 16 Jul 2008 14:39 Is it possible to template abstract methods in Java and then override them in the concrete classes. For example... class AbstractClass { public abstract <T extends SomeOtherClass> boolean doStuff(T blah); } class ConcreteClass extends AbstractClass { public boolean doStuff(AClassThatExtendsSomeOtherClass blah) { // now I don't need to cast that specific class } } I don't even know if something like this is possible and I wasn't able to find a solution online. Thanks for any insight.
From: conrad on 16 Jul 2008 14:48 On Jul 16, 2:39 pm, Jason Cavett <jason.cav...(a)gmail.com> wrote: > Is it possible to template abstract methods in Java and then override > them in the concrete classes. > > For example... > > class AbstractClass { > public abstract <T extends SomeOtherClass> boolean doStuff(T blah); > > } > > class ConcreteClass extends AbstractClass { > public boolean doStuff(AClassThatExtendsSomeOtherClass blah) { > // now I don't need to cast that specific class > } > > } > > I don't even know if something like this is possible and I wasn't able > to find a solution online. These aren't templates. These are generics. Perhaps that is what make your search unsuccessful. The normal way to do it is to make the interface or abstract class itself generic, not just the method, then override with a specific type. public interface Behaver <T extends Activity> { public boolean isStuff( T foo ); } // for some class Blah that is a subtype of Activity: public class BazBehaver implements Behaver <Blah> { @Override public boolean isStuff( Blah foo ) { return (foo != null); } } -- Lew
From: Lew on 16 Jul 2008 18:57
con...(a)lewscanon.com wrote: > public class BazBehaver implements Behaver <Blah> It would have been more idiomatically correct and self-documenting to call the class 'BlahBehaver' since it subtypes 'Behaver <Blah>'. 'BazBehaver' is a name that implies 'implements Behaver <Baz>'. -- Lew |