Lets assume I have class:
class A {
protected int x; // no public getter, setter
}
Now I want to extend class A, to overwrite 'x' variable. The answer would be:
public class SomeClass {
someMethod() {
A extendedClass = new MyExtension(5);
}
class MyExtension extends A {
public MyExtension(int x) {
super.x = x;
}
}
}
And my question: is there any possibility to do it without defining nested class separately? I mean something like this
someMethod() {
A extendedClass = new A() {
// here do something like super.x = 5;
}
}
I tried with Calling newly defined method from anonymous class but it won't let me instantiate A class. Also I dont want to use reflection.
I simpy dont want to define nested class just to overwrite one property. Origin of the problem is Spring-integration ImapMailReceiver, where I want to overwrite task scheduler. Like below:
final ImapMailReceiver imapMailReceiver = new ImapMailReceiver() {
super.setTaskScheduler(MYTASKSCHEDULERHERE);
}