0

My application is dependent on another applications abstract method implementation. I want help in how to access the implementation of that abstract method as the other application is only exposing us the Abstract interface which has the method definition.

So, my question is how to access the implementation of the abstract method, present in the abstract class. Do we have to use reflection for this ?

4
  • You want to call that method from derived class? If yes, just use super keyword, which points to the parent of extendend class Commented Jul 13, 2017 at 10:42
  • @MaciejTreder : No, i want to access the implementation in a class which is not a subclass of that abstract class. Commented Jul 13, 2017 at 10:56
  • So you need to use reflection. Commented Jul 13, 2017 at 10:57
  • Can you provide some pseudo-code to showcase your scenario, the structure of the classes, a bit? It is not that clear and it is also hard to give a precise answer other than just 'use technique xy'. However currently it sounds like you need to use reflection. With super you can such a method and with reflection you are be able to edit some stuff of it at runtime. In general an extending class is not allowed to change code of its super class, that somehow breaks the inheritance concept. For example what if other classes also extend from that class etc. Commented Jul 13, 2017 at 11:11

1 Answer 1

1

You can do that but you'll need to instantiate a class that extends the abstract class.

Method m = AbstractClass.class.getDeclaredMethod("MethodName", Integer.class);
m.setAccessible(true);
m.invoke(new InstanceOfAbstractClass(),"parameter");

This is useful just if you need to access a private method of the abstract class. If this is not the case, you'll need to call the method from the child instance.

Sign up to request clarification or add additional context in comments.

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.