mardi 14 novembre 2017

Java Inheritance and Reflections

While working on a customized, java based, trading platform, I came across this peculiar behavior in code, which I want to discuss.

So there were 2 classes in hierarchy, superClass and the childClass (extending from the superClass). The superClass (not abstract) had 5-6 methods and the childClass had overridden 2 of these methods.

Now an object of childClass was dynamically loaded through reflection and a method was called on it which existed in the superClass but the childClass had not overridden. In other words, this method was not to be seen in the childClass. Now when we executed this code,it blew up with noSuchMethodException.

We thought, it must work because it should call the method from its super class. However, in order to do a quick fix, I simply override the method in childClass, and called super from inside the braces, which seemed very odd, but it worked.

I want to know whether it is a normal behavior with Reflection or something seems wrong here. Also, I want to know whether the solution I tried was a proper one or are there better ways of dealing with such scenarios?





Aucun commentaire:

Enregistrer un commentaire