java - Using generics that extend a class -
i have class this:
public abstract class someclass<e extends anotherclass> { private e somevariable; public e formatvariable(){ e formatted = somevariable.format(); //format method in anotherclass, returns anotherclass return formatted; } }
eclipse gives me error, "cannot convert anotherclass e"
i confused because in class decleration made e anotherclass or subclass of it, or @ least understanding of it.
what's going on here, , how can fix error?
format()
returns anotherclass
, don't want any anotherclass — want e
specifically. if had this?
public class anotherclassred extends anotherclass { @override public anotherclass format() { return new anotherclassblack(); } } public class anotherclassblack extends anotherclass { // note: *not* subclass of anotherclassred ...
with scenario, if things compiled formatvariable()
return formatted instance anotherclassred, instance anotherclassblack; result classcastexception.
it sounds want format()
return e
, not anotherclass
.
Comments
Post a Comment