Overriding interface methods on objects you don't instantiate yourself  
Author Message
Jacques-Henri





PostPosted: 2004-4-16 22:46:00 Top

java-programmer, Overriding interface methods on objects you don't instantiate yourself Assume I have a class inherited from JTextComponent. I don't know what type
of Document will be used as its model, however I want to add functionnality
in the insertString() method of this Document instance.

How do I do this?

If that's not clear, I'd like to do something like this:

public class MyTextComponent extends JTextComponent {
...
protected Document createDefaultModel() {
return super.createDefaultModel() { // doesn't work, ofc

public void insertString(int offset, String str, AttributeSet a)
{
doSomething();
super.insertString(offset, str, a);
}

};

}

}



 
Michael Borgwardt





PostPosted: 2004-4-16 23:29:00 Top

java-programmer >> Overriding interface methods on objects you don't instantiate yourself Jacques-Henri wrote:

> Assume I have a class inherited from JTextComponent. I don't know what type
> of Document will be used as its model, however I want to add functionnality
> in the insertString() method of this Document instance.
>
> How do I do this?
>
> If that's not clear, I'd like to do something like this:
>
> public class MyTextComponent extends JTextComponent {
> ...
> protected Document createDefaultModel() {
> return super.createDefaultModel() { // doesn't work, ofc

No, it can indeed not be done that way. Instead you (have to) use the
Decorator pattern, i.e. write a "wrapper" class that takes the object in
question and forwards all calls to it, modifying those you need modified.