Overriding interface methods on objects you don't instantiate yourself

Discussion in 'Java' started by Jacques-Henri, Apr 16, 2004.

  1. 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);
    }

    };

    }

    }
     
    Jacques-Henri, Apr 16, 2004
    #1
    1. Advertising

  2. Re: Overriding interface methods on objects you don't instantiateyourself

    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.
     
    Michael Borgwardt, Apr 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex VanderWoude
    Replies:
    7
    Views:
    405
    Amaury Forgeot d'Arc
    Mar 30, 2005
  2. www
    Replies:
    1
    Views:
    304
  3. www
    Replies:
    1
    Views:
    297
  4. Jason
    Replies:
    0
    Views:
    229
    Jason
    Jul 6, 2004
  5. Jan Roslind
    Replies:
    10
    Views:
    225
    Caleb Clausen
    Feb 15, 2010
Loading...

Share This Page