Posted By:
Paul_Connaughton
Posted On:
Tuesday, February 19, 2002 03:08 AM
You can implement an overridden method to return a subclass of the return type, but you can not specify that inthe method header. The return type must be the same as the parent class.
The following example will allow you to return a HashMap as a Map, but you have to cast it to access HashMap functionality.
public class SomeSubclass extends AbstractClass{
public Map overRiddenMethod(){
HashMap map = new HashMap();
return map;
}
}
public static void main( String args[] ){
AbstractClass myClass = new SomeSubClass();
Map map = myClass.overRiddenMethod();
if ( map instanceof HashMap ){
//now you know that you have a HashMap
HashMap hashMap = (HashMap) map;
}
}
}