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 3 of 3

Thread: Generics and Dynamic Instantiation Problem

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Generics and Dynamic Instantiation Problem

    Can anyone help me to fix the code in the Main so that is works as intended?
    Gurus will know what I'm after.
    TIA

    Please see below:

    public abstract class MyBaseElement 
    {...}
     
    public class MyElementA extends MyBaseElement
    {...}
     
    public class MyElementB extends MyBaseElement
    {...}
     
    public class MyElementC extends MyBaseElement
    {...}
     
    public interface Mapper<T>
    {...}
     
    public class MyMapperA implements MyMapper<MyElementA>
    {...}
     
    public class MyMapperB implements MyMapper<MyElementB>
    {...}
     
    public class MyMapperC implements MyMapper<MyElementC>
    {...}

    // this method returns an instance of
    // one of the child classes: MyElementA, MyElementB, MyElementC, etc...

    public MyBaseElement getSpecificElement() {...}

    // this method returns a specific mapper based on the element
    public Mapper<? extends MyBaseElement> getSpecificMapper(MyBaseElement element)
    {
        if (element instanceof MyElementA) return new MyMapperA;
        if (element instanceof MyElementB) return new MyMapperB;
        if (element instanceof MyElementC) return new MyMapperC;
        ...
    }

    // Main
     
    ...
     
    MyBaseElement element = getSpecificElement();
     
    // the line below compile
    Class<? extends MyBaseElement> myElement = element ;
     
    //the line below does not compile: can not convert from MyBaseElement to Mapper<? extends MyBaseElement>
    // Any ideas, please?
    Mapper<? extends MyBaseElement> myMapper = getSpecificMapper(myElement ) 
     
    myMapper.doStuff();
     
    ...
     
    // end-Main


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Generics and Dynamic Instantiation Problem

    Quote Originally Posted by marcosmarcos
    Can anyone help me to fix the code in the Main so that is works as intended?
    Gurus will know what I'm after.
    I have no idea what you are after.

    Firstly I would recommend posting something we can compile.
    Secondly, please give us an indepth description of your problem.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Generics and Dynamic Instantiation Problem

    I agree with JavaPF here, somewhat confusion this.

    However, I'm not sure this will work, into the method getSpecificMapper you're passing in a Class<? extends MyBaseElement> but in the method signature it takes a MyBaseElement, not a Class.

    // Json

Similar Threads

  1. Generics problem
    By ankur.trapasiya in forum Collections and Generics
    Replies: 1
    Last Post: January 22nd, 2011, 02:52 PM
  2. Dynamic compilation problem
    By hemanth.yerra in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 29th, 2010, 10:51 AM
  3. [SOLVED] Dynamic numbber of JPanels
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 14th, 2010, 02:44 AM
  4. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 02:25 AM
  5. dynamic method problem...
    By Ranger-Man in forum Object Oriented Programming
    Replies: 8
    Last Post: September 7th, 2009, 04:22 PM

Tags for this Thread