Displaced Hoser
Displaced Hoser

Reputation: 911

Defining variables' classes at runtime via polymorphism

Having a hard time searching for this problem because I'm not entirely sure how to define it. Please bear with me.

The best I can phrase the question: In Java, how do I create variables whose classes are defined at runtime via polymorphism, rather than predefined in the code?

Perhaps I can best define this by example: Suppose I have an abstract superclass Superhero with the subclasses Thug, Psionic, Shooter and Gadgeteer. I want to read from a CSV data file whose line entries are individual superheroes; among the variables the file lists for each superhero is the which class they belong to. How can I then assign each superhero to the class listed in the file? What I've done so far includes the following.

I create an array of subclass types as follows:

numberOfClasses = 4; // constant
Superhero[] heroType = new Superhero[numberOfClasses];
heroType[0] = new Thug();
heroType[1] = new Psionic();
heroType[2] = new Shooter();
heroType[3] = new Gadgeteer();

Then we have a for loop that walks through each line of the file. For each line, it reads the name of the hero into the variable tempClassName, and then begins a nested loop that does this:

for (int index=0; index<numberOfClasses; index++)
{
    Class heroClass = heroType[index].getClass();

    if (tempClassName.equals(heroClass.getName()))
    {
        Superhero newHero = new heroClass; // problem line
    }
}

The last problem line is supposed to create a new Superhero object whose subclass is whatever's in the variable heroClass. But you can probably see that this doesn't work. Defining it as "new heroClass" gives a syntax error because it wants a method heroClass(). But "new heroClass()" is also a problem because heroClass isn't a method. Basically, how can I use the results of getClass() to be the (sub)class type of my newly created variable? Do I need to do something with the constructors in each of the subclasses?

Upvotes: 0

Views: 283

Answers (3)

kundan bora
kundan bora

Reputation: 3889

suppose you have list of class name className[]

 for(int i =0;i<className.length;i++)
 {
  SuperHero superHero = (SuperHero)Class.forName("PACKAGE_NAME" + className[i]).newInstance();

  //use this superhero where you want. you can also save this superhero in ArrayList of type SuperHero.
  }

Upvotes: 1

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

    Superhero[] heroType = new Superhero[]{
            new Thug(),
            new Psionic(),
            new Shooter(),
            new Gadgeteer()
    };

    for (int index = 0; index < heroType.length; index++) {
        Class heroClass = heroType[index].getClass();

        if (tempClassName.equals(heroClass.getName())) {
            Superhero newHero = (Superhero) heroClass.newInstance();
        }
    }

Upvotes: 1

Louis Wasserman
Louis Wasserman

Reputation: 198211

Assuming that you're sure this type of reflection is a good idea, you could do

heroClass.newInstance();

Upvotes: 0

Related Questions