Let ClassA represent an abstract class.
I'm wondering if it's possible to do something like this in Java:
public static abstract class ClassA {
abstract void foo();
}
private ClassB extends ClassA a = new ClassA() {
// define abstract methods here
foo() { System.out.println("in anon!"); }
// define OTHER variables here
public int varB = 10;
}
// later... varB is not defined in ClassA, but it is in a.
// how can I access varB from a?
System.out.println(a.varB);
I want to make a special note: notice how I'm printing varB. Say varB was NOT defined in ClassA, only in the anonymous inner class.
I have a lot of abstract classes wherein I define their abstract methods on the fly when I create them. However, I would also like to treat these classes as their own type because sometimes I need to access variables from that class that are specific to that version of the object.
Is there a way of doing something similar to what I've shown above, or do I need to create a "ClassB" that extends from "ClassA" every time?
Is there some type of workaround?
Thanks