OCP Generics
Posted: Thu Nov 09, 2017 3:24 pm
Hello , I ve read a lot about generics but still dont understand it.
I dont understand why i get the ClassCastException. Thought i typed the ArrayList to TextBook12.
However, I cannot insert anything, not even an object of type TextBook12.
Why and how can i resolve it.
Thanx in advance
Code: Select all
import java.util.*;
public class Book12{
public static void main(String args[])
{
BookList<TextBook12> list = new BookList<>();
list.add(new TextBook12());
System.out.println(list.count);
}
}
class TextBook12 extends Book12{ }
class BookList<TextBook12> extends ArrayList<TextBook12>
{
public int count = 0;
}
However, I cannot insert anything, not even an object of type TextBook12.
Why and how can i resolve it.
Thanx in advance