Rohit Banga
Rohit Banga

Reputation: 18926

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.

Upvotes: 1

Views: 952

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503290

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.)

Upvotes: 5

Gregory Pakosz
Gregory Pakosz

Reputation: 70244

You can call Class.newInstance

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

Upvotes: 1

Related Questions