views:

113

answers:

2
+1  Q: 

java polymorphism

Consider the following code:

public abstract class Base {
       public void getAnswer();
}

public class Derived1 extends Base {
       public void getAnswer() {
       }
}

public class Derived2 extends Base {
       public void getAnswer() {
       }
}


public class Main {

   public final int DERIVED1 = 1;
   public final int DERIVED2 = 2;

   public Base b;

   public static void Main() {

      int which_obj = which();

      switch (which_obj) {
      case DERIVED1: Derived1 derived1 = new Derived1();
                     // construct object
                     b = derived1;
                     break;
      case DERIVED2: Derived1 derived1 = new Derived1();
                     // construct object
                     b = derived2;
                     break;
      }

      b.getAnswer();
   }
}

here we are using a switch case to decide what object to construct and accordingly we construct it and assign it to b so as to use polymorphism.

what advantage is polymorphism giving us here?

Now is there a method by which we can avoid the switch case. say: there is a mapping from int to String that returns "Derived1" or "Derived2". given the string containing the name of the class. now can i construct an object given the String containing the name of the class. something like:

Base b = construct_obj(str);

the library call construct_obj automatically locates the class of obj from the bytecodes and returns a reference to the object. then i can avoid the switch case. typically i would be having 100's of Derived classes.

+4  A: 

Well, you could create a Map<Integer, Class<? extends Base>> and then use Class.newInstance to create the instance with reflection. It wouldn't perform as well, and there are various errors which wouldn't be exposed until execution time, but it would work.

Here's some sample code.

import java.util.*;

abstract class Base {}
class Derived1 extends Base {}
class Derived2 extends Base {}

public class Test
{
    public static void main(String[] args)
    {
        Map<Integer, Class<? extends Base>> map = 
            new HashMap<Integer, Class<? extends Base>>();

        map.put(1, Derived1.class);
        map.put(2, Derived2.class);

        int which = 2; // For example

        Class<? extends Base> clazz = map.get(which);
        if (clazz == null)
        {
            // Invalid choice. Do whatever.
        }
        else
        {
            try
            {
                Base base = clazz.newInstance();
                // Use base
            }
            catch (InstantiationException e)
            {
                // Handle exception or whatever
            }
            catch (IllegalAccessException e)
            {
                // Handle exception or whatever
            }            
        }
    }
}

Note that if you've got a contiguous range of integers, you could use an array instead of a map.

Another alternative is to have factory instances rather than using constructors - this would be "safer" (in terms of not deferring errors) but probably significantly more code. (In C# it would be easy using lambda expressions; Java 7 may make this a preferable solution too.)

Jon Skeet
This is completely valid answer and everything but I just have to say that every time I see Java that's not formatted by JCC rules it makes me cringe. One of those little things, you know, nothing important.
Esko
@Esko: Including 8 character indentation? Ick. Personally I don't think that Sun should have included formatting in the conventions - naming makes a lot of sense, as that comes out in an API, but I think it makes sense for different companies to decide their own preferred layout, especially as it's such a "religious" argument. At work we use the Sun conventions, and I've become somewhat used to them - but by and large I still prefer the "brace on its own line" style.
Jon Skeet
Obligatory brace humor: Some developers have argued [against this]. The author of this FAQ sees no clear advantage to adopting this approach and believes that said developers also put their curly braces on the wrong line. :-) http://junit.sourceforge.net/doc/faq/faq.htm#organize_1
trashgod
Jon, quoting http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html **"Four** spaces should be used as the unit of indentation. [..] Tabs must be set exactly every 8 spaces (not 4)." We don't use tabs at all at my work *(I think the reasoning was 'maximum charset compatability' or some such)* which means multiples of four everywhere. But as said, it's just something that hurts my eye in the same way as some people get annoyed if the word *Grocers* is missing an apostrophe.
Esko
@Esko: I do apologise. I could have sworn it was 8 spaces, which ends up being quite horrible. I wonder where I got that impression from. I still think it was a shame to specify it though :) (Oh, and I agree on the "spaces not tabs" issue :)
Jon Skeet
+1  A: 

You can call Class.newInstance

Class cl = Class.forName("com.stackoverflow.Derived1");
Base b = cl.newInstance();
Gregory Pakosz