18
4
|
Suppose I have a class A:
And an instance of that class:
Is there any way to override the
method1() only for anA ? This question arises when I write a small painting program in which I have to extend the JPanel class several times just to make minor changes to the different panels that have slightly different characteristics. | ||||
|
43
|
You can do the following:
This is the same as:
Only with the exception that in this case
myA can be reused. That's not possible with anonymous classes. | ||||||||||||
|
11
|
You can create a an new anonymous class on the fly, as long as you are using the no-arg constructor of your class
A :
Note that what you want to do is very close to what is known as closure, which should come along with next release 8 of Java SE.
|