Combining generic type parameters with interfaces in Java -
as many inheritance problems, find hard explain want do. quick (but peculiar) example should trick:
public interface shell{ public double getsize(); } public class tortoiseshell implements shell{ ... public double getsize(){...} //implementing interface method ... public tortoise gettortoise(){...} //new method ... } public class shellviewer<s extends shell>{ s shell; public shellviewer(s shell){ this.shell = shell; ... } } public class tortoiseshellviewer<t extends tortoiseshell> extends shellviewer{ public tortoiseshellviewer(t tshell){ super(tshell); //no problems here... } private void removetortoise(){ tortoise t = tshell.gettortoise(); //error: compiler can not find method in "shell" ... } }
the compiler not recognise want use specific implementation of shell gettortoise()
. have gone wrong?
based on you've given here, problem that:
public class tortoiseshellviewer<t extends tortoiseshell> extends shellviewer
does not specify shellviewer
(which generic) correctly. should be:
public class tortoiseshellviewer<t extends tortoiseshell> extends shellviewer<t>
Comments
Post a Comment