Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Overriding interface methods on objects you don't instantiate yourself

Reply
Thread Tools

Overriding interface methods on objects you don't instantiate yourself

 
 
Jacques-Henri
Guest
Posts: n/a
 
      04-16-2004
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);
}

};

}

}



 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      04-16-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use 2 dimensional array to instantiate new objects eomer Java 9 12-05-2005 09:01 PM
Instantiate objects Daniel Walzenbach ASP .Net 6 07-17-2005 03:48 PM
Overriding methods in classes you don't control Alex VanderWoude Python 7 03-30-2005 02:20 AM
How to Instantiate object pointers that are members of other objects . Glenn Serpas C++ 5 08-09-2004 01:11 PM
don't you ever, don't you ever, lower yourself, forgetting all your standards Jason ASP .Net Web Controls 0 07-06-2004 12:50 PM



Advertisments