#Using Reflection to get superclass constructor

10 messages · Page 1 of 1 (latest)

cursive flame
#

It's my lovable dumb self again.

So, I got an abstract parent class that has two protected constructors:

  • one that takes a DataInputStream and a boolean
  • one that takes a String

and a bunch of implementing child classes that extend this abstract class.

Each child class uses the constructor that takes a String, like so:

public ImplChildClass(String one, String two) {
  super(one);
  this.two = two;
}

All super standard. Now, I want to be able to construct an implementing class using the abstract classes constructor that takes the DataInputStream and boolean arguments. Google tells me I should be able to do

Constructor<? extends AbstractParentClass> constructor = ImplChildClass.getClass().getDeclaredConstructor(DataInputStream.class, boolean.class);
constructor.setAccessible(true);
constructor.newInstance(dataInputStream, true);

but I'm getting java.lang.NoSuchMethodException when I do that. So, what am I doing wrong?

ruby wharfBOT
#

This post has been reserved for your question.

Hey @cursive flame! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

ruby wharfBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

cursive flame
#

Still having some issues with this

cursive flame
#

So no matter what I do, I cannot get the superclass constructor from the implementing class, so I have to be doing something wrong, or it isn't possible

mighty acorn
#

ImplChildClass.class

#

not ImplChildClass.getClass()

#

And does the child class have the costructor with DataInputStream and boolean?

#

because constructors aren't inherited