Abstract inner classes

T

Timo Nentwig

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
 
C

Chris Smith

Timo said:
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
 
T

Timo Nentwig

Chris said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top