Abstract inner classes

Discussion in 'Java' started by Timo Nentwig, Apr 13, 2004.

  1. Timo Nentwig

    Timo Nentwig Guest

    Hi!

    This is the problem: An object gets data passed in the constructor and
    needs to handle it but does not know what kind of data it is passed to.
    So it additionally needs an Adapter that implements a common interface
    which does wrap that data. I thought about something like this:

    public DataProcessor
    {
    // needs to be implemented
    protected astract class Adapter
    {
    // I'd prefer it if would be static
    protected abstract Object get(int index);
    }

    protected Object data;

    public DataProcessor(Object data)
    {
    this.data = data;
    }

    public void process()
    {
    for (int i = 0; ...)
    blah((new Adapter()).get(i));
    }
    }



    Which would be instantiated like this:

    DataProcessor dp = new DataProcessor(data)
    {
    protected class DataProcessor.Adapter
    {
    protected Object get(int index)
    {
    wrap(DataProcessor.this.data);
    }
    }
    });


    But I don't quite get it working - and I can instantiate DataPRocessor
    without implementing the Adapter...somebody to give me some coaching on
    Inner Classes? Other solutions to the problem are welcomed as well...(
    as long as it is not DataProcessor.setAdapter():)

    TIA
    Timo
    Timo Nentwig, Apr 13, 2004
    #1
    1. Advertising

  2. Timo Nentwig

    Chris Smith Guest

    Timo Nentwig wrote:
    > This is the problem: An object gets data passed in the constructor and
    > needs to handle it but does not know what kind of data it is passed to.
    > So it additionally needs an Adapter that implements a common interface
    > which does wrap that data.


    Okay...

    > I thought about something like this:
    >
    > public DataProcessor
    > {
    > // needs to be implemented
    > protected astract class Adapter
    > {
    > // I'd prefer it if would be static
    > protected abstract Object get(int index);
    > }
    >
    > protected Object data;
    >
    > public DataProcessor(Object data)
    > {
    > this.data = data;
    > }
    >
    > public void process()
    > {
    > for (int i = 0; ...)
    > blah((new Adapter()).get(i));
    > }
    > }


    This probably won't work the way you are expecting. Your code saying
    "new Adapter()" in process() will create an instance of
    DataProcessor.Adapter. Class definitions may not be overridden by
    subclasses. If you need to override something, it needs to be a method.
    How about:

    public abstract class DataProcessor
    {
    protected interface Adapter
    {
    // whatever
    }

    private Object data;

    public DataProcessor(Object data)
    {
    this.data = data;
    }

    public void process()
    {
    Adapter adapter = getAdapter();
    // whatever
    }

    protected abstract Adapter getAdapter();
    }

    And then:

    DataProcessor dp = new DataProcessor(data) {
    protected Adapter getAdapter()
    {
    return new Adapter() {
    // whatever
    };
    }
    }

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Apr 13, 2004
    #2
    1. Advertising

  3. Timo Nentwig

    Timo Nentwig Guest

    Chris Smith wrote:
    > DataProcessor.Adapter. Class definitions may not be overridden by


    :-(

    > subclasses. If you need to override something, it needs to be a method.
    > How about:


    Looks like a good solution. Despite I'd prefer if a concrete class A
    could have an abstract class A.B which must be implemented in order to
    instantiate A...

    Thanks!
    Timo
    Timo Nentwig, Apr 14, 2004
    #3
    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. lonelyplanet999
    Replies:
    1
    Views:
    2,193
    VisionSet
    Nov 13, 2003
  2. DaKoadMunky
    Replies:
    4
    Views:
    537
    Lee Weiner
    Apr 20, 2004
  3. Razvan
    Replies:
    5
    Views:
    11,274
    Dale King
    Jul 27, 2004
  4. Carlo v. Dango
    Replies:
    14
    Views:
    1,010
    Alex Martelli
    Oct 19, 2003
  5. Pyenos
    Replies:
    2
    Views:
    374
    Pyenos
    Dec 27, 2006
Loading...

Share This Page