0

I need to be able to call the constructor of an inheriting class depending on what is input for the method newInstance(String kind)... my code currently looks like this:

public class Animal {


public String kind;
public String integument;

protected Animal(){

}

public Animal newInstance(String kind){
    kind x = new kind();
    return x;
}

So for instance, if the String put in was "owl", I want it to call the constructor in my previously created owl class and create a new owl instance. The owl class looks like:

public class Owl extends Bird{

public Owl(){
    this.kind = "owl";
}

public void newOwl(){
}

@Override
public String sound(){
    return "'Hoot'";
}


}

Right now I get an error that says, "class kind is not found" because it is looking for a class called kind, rather than waiting for an input and looking for a class called the same thing as the string input.

How can I rewrite this code so that the input in that method will look for a class by that name and create a new object of that type? Thanks!

5
  • 3
    You could use Class.forName(kind).newInstance(), which does exactly what you've described. But that's probably not a very good solution to your problem. What problem are you actually trying to solve? Jan 18, 2015 at 20:46
  • 1
    Maybe you want a simple switch using kind? switch(kind) { case "owl": return new Owl(); } Jan 18, 2015 at 20:46
  • You should probably implement Animal.newInstance() as a Factory Method. A little more detail would help us narrow down the wide range of possibilities here.
    – markspace
    Jan 18, 2015 at 20:47
  • I cannot make much sense of your code. Could you please re-check (and also re-format) it? For example, the newInstance method you show calls a kind constructor and returns its result as an Animal. That looks definitely wrong. A complete example would help.
    – 5gon12eder
    Jan 18, 2015 at 20:49
  • In real applications, you would normally need to initialize each subclass in a different way (i.e. passing different arguments to the constructors, or using some other logic as opposed to just a class name). So the switch becomes the best way. Jan 18, 2015 at 20:49

1 Answer 1

3

You probably want to use what's called a Factory, in another class entirely or as a static method on Animal:

public class Animal {
    public static Animal getInstance(final String kind) {
        if(kind== null) {
             throw new IllegalArgumentException("Must provide a name");
        }
        switch(kind.toLowerCase()) {
           case "owl": return new Owl();
           case "deer": return new Deer();
           default: throw new IllegalArgumentException("Can't create a " + kind);
        }
    }
}
2
  • Calling String.toLowerCase() without specifying a locale is asking for trouble.
    – dnault
    Jan 18, 2015 at 22:26
  • @dnault Indeed depending on the inputs, but I was trying not to confuse the issue with that. Very good point though.
    – Todd
    Jan 18, 2015 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.