How to redefine a default implementation of an interface for a subclass? #1620
Replies: 4 comments
-
You can redefine it with a Java agent. Have a look at |
Beta Was this translation helpful? Give feedback.
-
Thanks. Got caught up in something and i'll try and get back to this thread later. |
Beta Was this translation helpful? Give feedback.
-
Thanks Rafael. (sorry, got caught up with some work and just got back to this) Let me rephrase my requirement a bit:
The above need to have during runtime. Sample code: interface TheBase {
default void baseAction1() {}
default void baseAction2(TheBase other) {}
}
class TheImpl1 implements TheBase {
public void action() {}
}
class TheImpl2 implements TheBase {
public void action() {}
}
class App {
void run() {
redefineTheImpl1_baseAction2_version1();
TheImpl1 impl1_1 = new TheImpl1();
TheImpl2 impl2_1 = new TheImpl2();
impl1_1.baseAction2(impl2_1);
redefineTheImpl1_baseAction2_version2();
TheImpl1 impl1_2 = new TheImpl1();
TheImpl2 impl2_2 = new TheImpl2();
impl1_2.baseAction2(impl2_2);
}
void redefineTheImpl1_baseAction2_version1() {}
void redefineTheImpl1_baseAction2_version2() {}
} |
Beta Was this translation helpful? Give feedback.
-
it turns out mockito already provides what i need, thanks! |
Beta Was this translation helpful? Give feedback.
-
Is there a way for me to redefine the
baseAction
forMyClass
?Assuming
MyClass
's constructor is unknown at this stage, or I cannot useMyClass.getConstructor().newInstance()
to initialise it.(the reason is that I only vend
MyInterface
and the client will be implementing their own subclasses likeMyClass
so I have no control how the instances of those subclasses are initialised)Beta Was this translation helpful? Give feedback.
All reactions