In Java is it possible to dynamically create anonymous subclass instance given only instance of parent class?
The pattern code I'm trying to implement looks like this:
public interface IStringCarier { public String getStr(); }
public static IStringCarier introduce(Object victim, final String str) {
// question subject
}
public class AAA { }
public static void main() {
AAA aaa = new AAA();
assert !(aaa instanceof IStringCarier);
IStringCarier bbb = introduce(aaa, "HelloWorld");
assert aaa == bbb;
assert "HelloWorld".equals(bbb.getStr());
}
There're actually 2 more requirements-questions regarding this code:
(2) Not just create subclass instance, but also reassign prototype instance to the newly created instance (2nd assert in the code).
(3) Introduce the subclass to some specific interface.
I doubt this is possible, but I'm novice with Java, so...