4

I wrote the question as a comment in the code, I think its easier to understand this way.

public class Xpto{
    protected AbstractClass x;

    public void foo(){

       // AbstractClass y = new ????? Car or Person ?????

       /* here I need a new object of this.x's type (which could be Car or Person)
          I know that with x.getClass() I get the x's Class (which will be Car or 
          Person), however Im wondering how can I get and USE it's contructor */

       // ... more operations (which depend on y's type)
    }

}

public abstract class AbstractClass {
}

public class Car extends AbstractClass{
}

public class Person extends AbstractClass{
}

Any suggestions?

Thanks in advance!

1
  • Are you trying to learn reflection, or is this a part of your application? If latter, there are better and safer alternatives for constructing an object of desired type at runtime, e.g. factories. Commented Apr 24, 2010 at 0:08

2 Answers 2

5

First of all, BalusC is right.

Secondly:

If you're taking decisions based on the class type, you're not letting the polymorphism do its job.

Your class structure may be wrong ( Like Car and Person should not be in the same hierarchy )

You could probably create an interface and code to it.

interface Fooable {
     Fooable createInstance();
     void doFoo();
     void doBar();
}

class Car implements Fooable {
     public Fooable createInstance() {
          return new Car();
     }
     public void doFoo(){
         out.println("Brroooom, brooooom");
     }
     public void doBar() {
          out.println("Schreeeeeeeekkkkkt");
      }
}
class Person implements Fooable {
     public Fooable createInstance(){   
         return new Person();
      }
      public void foo() {
           out.println("ehem, good morning sir");
      }
      public void bar() {
          out.println("Among the nations as among the individuals, the respect for the other rights means peace..");// sort of 
      }
}

Later ...

public class Xpto{
    protected Fooable x;

    public void foo(){
         Fooable y = x.createInstance();
         // no more operations that depend on y's type.
         // let polymorphism take charge.
         y.foo();
         x.bar();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1 I agree with using a factory method to create new instances. It's a bit like clone, only that it returns a blank instead of a copy.
@Chris: Yeap, clone could be used also, when you don't know anything about the object until runtime ( which is rate in Java, there you know at least it's some type of Fooable ) but when you have: Object o = ???; Object copy = o.clone() could be useful.
Unfortunately, because Object.clone is protected, the only way you can do that with objects of unknown type is to invoke clone by reflection. :-P And if you're gonna use reflection, you may as well reflect on the factory method, or even the constructor. :-P
@Chris: But the type is partially know. It is a Fooable/AbstractClass, which can redefine clone() as public.
3

If the class has a (implicit) default no-arg constructor, then you can just call Class#newInstance(). If you want to obtain a specific constructor, then use Class#getConstructor() wherein you pass the parametertypes to and then call Constructor#newInstance() on it. The code in blue are actually links, click them to get the Javadoc, it contains detailed explanation about what exactly the method does.

To learn more about reflection, head to the Sun tutorial on the subject.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.