qnhant5010
qnhant5010

Reputation: 305

Inner class's property having outer class's generic type

I have a class Item which will be used as generic type in the following class:

abstract class AbstractClass<I extends Item, V extends AbstractClass.Inner> {
    abstract class Inner {
        I item;
    }
}

Then I subclass AbstractClass also its Inner with Page extending Item mentionned above:

class ImpClass extends AbstractClass<Page, ImpClass.Inner> {
    class Inner extends AbstractClass.Inner {
        void method(){
             // Setup item
             // Printout class of item, which is Page
             item.callMethodOfPage(); // won't compile without conversion to Page
        }
    }
}

Theorically, item in ImpClass.Inner should be Page and has the method only in Page. Is it because i'm misssing something?

Upvotes: 1

Views: 39

Answers (1)

shmosel
shmosel

Reputation: 50716

AbstractClass.Inner is a raw type. You need to parameterize AbstractClass:

abstract class AbstractClass<I extends Item, V extends AbstractClass<I, V>.Inner> {
    abstract class Inner {
        I item;
    }
}

class ImpClass extends AbstractClass<Page, ImpClass.Inner> {
    abstract class Inner extends AbstractClass<Page, Inner>.Inner {
        void method(){
             // Setup item
             // Printout class of item, which is Page
             item.callMethodOfPage(); // compiles
        }
    }
}

Upvotes: 2

Related Questions