Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Instantiating a SuperClass Map from a SubClass

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Location
    Santa Ana, CA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Instantiating a SuperClass Map from a SubClass

    I am trying to design a abstract class that will instantiate a Map through a subclass, using an enum type that implements a interface. Contract will be that subclass must instantiate and set variables in it's own constructor.

    Here is an example:
    Here is the variable from the superclass (T extends Enum & PreferencesIDs)

    protected Map<T, String> stringList;

    and the abstract method:
    protected abstract void setPreferences();

    While I define the subclass I either can't compile the code or get a bunch of warnings.

    Attempt (Either through EnumMaps or HashMaps, using an pIDs enum that implements PreferenceIDs):

    stringList = new EnumMap<pIDs, String>(pIDs);
     
    stringList.put(pIDs.GENERAL_DATA_FILENAME_EXT, "atv");

    When I do this, I get this error:
    DataPreferenceManager.java:28: warning: [unchecked] unchecked assignment to variable stringList as member of raw type abstractPreferences.AbstractPreferences
    stringList = new EnumMap<pIDs, String>(pIDs.class);
    ^
    DataPreferenceManager.java:30: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
    stringList.put(pIDs.GENERAL_DATA_FILENAME_EXT, "atv");

    Now, is there anyway to do this a more correct way? Any obvious answers? I don't mind reading myself, I just haven't found any direct answers so far.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,296
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Instantiating a SuperClass Map from a SubClass

    Can you post a better defined example that reproduces the error you are encountering, or better yet what you want to achieve? From what I understand it seems you wish to constrain a preferences Map to a few specific values and use Generics to have a compile time rather than runtime error?