Java Inheritance and Types

Hello,

I have some super/sub classes, listed as follows:

UnitMgr

SpecialUnitMgr (child of UnitMgr)

Unit

SpecialUnit (child of Unit)

I'm designing a lot inventory system where my SpecialUnitMgr applet will have many instances of Unit. Right now I have the system set up so that UnitMgr contains an instance var declared as "Unit aUnit;". UnitMgr as a parent class, takes care of common code between the many different UnitMgr child classes.

Right now I have SpecialUnitMgr setup to use the inherited parent/super variable aUnit, and I have extended the functionality of Unit with SpecialUnit (added more instance vars, etc). I wish to use extended features of SpecialUnit within my SpecialUnitMgr class, but I am unable to do so because of type conflicts (i.e. i cant access SpecialUnit vars or methods from the instance aUnit because it is declared as Unit in UnitMgr).

I want to keep a single instance of Unit or SpecialUnit going, but I'm not sure how to do so.

I am wondering how to get around this problem, do I use Reflection? Is my design bad? I am trying to maintain the super classes as a core package.

Jeff

[1184 byte] By [foojiea] at [2007-10-3 3:30:38]
# 1

If I understand the problem correctly, you want generics. public class UnitMgr<U extends Unit>

{

protected U aUnit;

}

public class SpecialUnitMgr extends UnitMgr<SpecialUnit>

{

}

See http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html for more on generics.

YAT_Archivista at 2007-7-14 21:24:33 > top of Java-index,Java Essentials,Java Programming...