I have a base container who works with parent class.
My task – add A Child container with Child type. Just extend existing container with some specific logic for child only
But unfortunatelly it is prohibited. Example in code:
public class Test {
public static void main() {
Parent parent = new Parent();
ChildContainer childContainer = new ChildContainer();
childContainer.add(parent); // ERROR. Expected type is Child. But Parennt is given
}
public static class Parent { }
public static class Child extends Parent { }
public static class ParentContainer<T extends Parent> {
public void add(T item) {}
public T get() {return null;}
}
public static class ChildContainer extends ParentContainer<Child> { }
}
But this works with some warnings:
Parent parent = new Parent();
ParentContainer childContainer = new ChildContainer();
childContainer.add(parent);
The question is how cann use this line ot make me abble to add parennt items:
childContainer.add(parent); // ERROR. Expected type is Child.
8