File tree Expand file tree Collapse file tree
src/main/java/com/ab/generics/limitation Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .ab .generics .limitation ;
2+
3+ import java .util .AbstractList ;
4+
5+ /**
6+ * @author Arpit Bhardwaj
7+ */
8+ public class CustomArrayList extends AbstractList {
9+ private Object [] values ;
10+
11+ public CustomArrayList () {
12+ this .values = new Object [0 ];
13+ }
14+
15+ @ Override
16+ public Object get (int index ) {
17+ return values [index ];
18+ }
19+
20+ public boolean add (Object o ){
21+ Object [] newValues = new Object [size () + 1 ];
22+ for (int i = 0 ; i < size (); i ++) {
23+ newValues [i ] = values [i ];
24+ }
25+ newValues [size ()] = o ;
26+ values = newValues ;
27+ return true ;
28+ }
29+ @ Override
30+ public int size () {
31+ return values .length ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .ab .generics .limitation ;
2+
3+ import java .util .List ;
4+
5+ /**
6+ * @author Arpit Bhardwaj
7+ */
8+ public class CustomArrayListDemo {
9+ public static void main (String [] args ) {
10+ //below code compiles fine but throws ArrayStoreException at runtime
11+ /*String[] strings = new String[3];
12+ Object[] objects = strings;//as arrays are covariant and Reified Type
13+ objects[0] = 1;*/
14+
15+ //List customArrayList = new CustomArrayList();
16+ List <Integer > customArrayList = new GenericCustomArrayList <>();
17+
18+ //customArrayList.add("abc");//compiler will not tell for raw type customArrayList
19+
20+ customArrayList .add (0 );
21+ customArrayList .add (1 );
22+ customArrayList .add (2 );
23+
24+ System .out .println (customArrayList .get (0 ));
25+ System .out .println (customArrayList .get (1 ));
26+ System .out .println (customArrayList .get (2 ));
27+ //System.out.println(customArrayList.get(3));
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .ab .generics .limitation ;
2+
3+ import java .util .AbstractList ;
4+
5+ /**
6+ * Solution 1: to cast Object arrays to generic T[]
7+ * Solution 2:
8+ * @author Arpit Bhardwaj
9+ */
10+ public class GenericCustomArrayList <T > extends AbstractList <T > {
11+ private Object [] values ;
12+ //private T[] values;//Solution 1
13+
14+ public GenericCustomArrayList () {
15+ this .values = new Object [0 ];
16+ //this.values = (T[]) new Object[0];//Solution 1
17+ }
18+
19+ @ Override
20+ public T get (int index ) {
21+ //return values[index];//Solution 2
22+ return (T ) values [index ];
23+ }
24+
25+ public boolean add (T o ){
26+ Object [] newValues = new Object [size () + 1 ];
27+ //T[] newValues = (T[]) new Object[size() + 1];//Solution 1
28+ for (int i = 0 ; i < size (); i ++) {
29+ newValues [i ] = values [i ];
30+ }
31+ newValues [size ()] = o ;
32+ values = newValues ;
33+ return true ;
34+ }
35+ @ Override
36+ public int size () {
37+ return values .length ;
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .ab .generics .limitation ;
2+
3+ /**
4+ * @author Arpit Bhardwaj
5+ */
6+ public class InstanceOf <T > {
7+ @ Override
8+ public boolean equals (Object obj ) {
9+ //generics in instanceOf not allowed
10+ if (obj instanceof InstanceOf /*<T>*/ ){
11+ return true ;
12+ }
13+ return false ;
14+ }
15+
16+ public static void main (String [] args ) {
17+ System .out .println (new InstanceOf <>() instanceof Object );
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .ab .generics .limitation ;
2+
3+ import java .util .List ;
4+
5+ /**
6+ * @author Arpit Bhardwaj
7+ */
8+ public class Overloading {
9+ public void print (String param ){}
10+ public void print (Integer param ){}
11+ public void print (List <String > param ){}
12+
13+ //cannot do
14+ //public void print (List<Integer> param){}
15+ //public void print (List<List<Integer>> param){}
16+ }
Original file line number Diff line number Diff line change 1+ package com .ab .generics .limitation ;
2+
3+ /**
4+ * Compile time error: Generic class may not extend 'java.lang.Throwable'
5+ * @author Arpit Bhardwaj
6+ */
7+ public class UncompilableException /*<T>*/ extends Throwable {
8+
9+ public static void main (String [] args ) {
10+ try {
11+ throw new UncompilableException ();
12+ }catch (UncompilableException /*<T>*/ e ){
13+ e .printStackTrace ();
14+ }
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments