Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using reflection to call constructors of parent classes

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
let's say I've got an abstract class with a public constructor that takes a few arguments. This constructor is not overloaded by a subclass.

Given a non-abstract class that extends the above class, I'd like to find all of the constructors I can call to instantiate this object. Included in this list should be the above constructor.

[tt]myClass.getConstructors()[/tt] will only return the public constructors for that particular class, not the public constructors for the parent classes. I can't use [tt]myClass.getSuperclass().getConstructors()[/tt] either, as that will reference the abstract class as the declaring class, and I won't be able to instantiate the object.

How exactly would I go about using reflection to find all of the possible public constructors, for the desired class as well as for all parent classes?

If you want, I could post some test code to demonstrate exactly what I mean.

Liam Morley
lmorley@wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
Liam, I don't believe this is possible to do, or if there is I cannot seem to find a way to do it using reflection.



--------------------------------------------------
Free Database Connection Pooling Software
 
Neither have I, but that doesn't seem to make sense:/ I thought you could do anything at run-time that you can at compile time... I'm not sure why that's not the case. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Then only way I can see to instantiate an object at runtime dynamically is using the Class.forName() method, or the Constructor.newInstance() method - which as you pointed out cannot be called on the abstract class.

Maybe you could change your design method to not use abstract classes, or use some setter methods in the class extending the abstract class to do the constructor work in it, or maybe use interfaces rather than abstract classes.
Oh, for C++'s multiple inheritence !



--------------------------------------------------
Free Database Connection Pooling Software
 
This is actually for a development tool where developers can create objects to help visualize their class design.. I'm just trying to accomodate all different types of constructors for that reason, not so much of my own design.<BR><BR>I was hoping for something magical like Constructor.setDeclaringClass() (which of course doesn't exist)... but it's surprising to me that Class.getConstructors() doesn't return all of the valid constructors which have been inherited. Aren't inherited methods returned with Class.getMethods() returned? Why should it be different for constructors? <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top