1

Let’s say we have an abstract class like this one:

abstract class Table<T extends Table<T, P>, P extends Menu<P>> { }

How to correctly declare its implementation?

public class TableImpl extends Table< **SOMETHING GOES HERE** > { }

I tried some variants and the only workaround I’ve found was to create additional classes:

public abstract class AbstractMenu extends Menu<AbstractMenu> { }
public abstract class AbstractTable extends Table<AbstractTable, AbstractMenu> { }

Then it’s possible to declare TableImpl like this:

public class TableImpl extends AbstractTable { }

EDIT 1

Based on Ingo's answer it now looks like this:

public class MenuAndTableTest {

    public abstract class Table<X extends Table<X>> {

        public abstract <M extends Menu> M getParent();

        public abstract X getThis();
    }

    public abstract class Menu<T extends Menu<T>> {

    }

    public class MenuImpl extends Menu<MenuImpl> {

    }

    public class TableImpl<M extends Menu> extends Table<TableImpl<M>> {

        M parent;

        public TableImpl(M parent) {
            this.parent = parent;
        }

        @Override
        public TableImpl getThis() {
            return this;
        }

        @Override
        public M getParent() {
            return parent;
        }

    }

    @Test
    public void Test() {
        // given
        MenuImpl menuImpl = new MenuImpl();
        // when
        TableImpl<MenuImpl> tableImpl = new TableImpl<>(menuImpl);
        // then
        assertEquals(tableImpl, tableImpl.getThis());
        assertEquals(menuImpl, tableImpl.getParent());
    }
}

But now some extra logic comes to Table implementations - not exactly what I wanted.

8
  • 1
    So you want a table of tables and menus which are menus of menus? Commented Apr 16, 2015 at 15:40
  • 2
    It's not very clear what you want. Commented Apr 16, 2015 at 15:48
  • @Boris Menu is some Page Object containing Table in it. Table should be able to return it's parent (Menu) in method like P getParentPage() { return parentMenu; } Commented Apr 16, 2015 at 15:50
  • @Isaiah The main idea - table of tables with its menu. Commented Apr 16, 2015 at 15:59
  • 3
    Please, please, please rethink your logic! Commented Apr 16, 2015 at 16:06

1 Answer 1

3

If you want a table that can conatin a table that contains menus, why don't you declare it as

class Table<X>

and instantiate with

Table<Table<Menu>>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.