java - Cast to inherited class from ArrayList of base class -


i have 2 classes , b , base class , b inherited a.

class {     int foo=10; }  class b extends {      int bar=100; } 

i declare arraylist of class , insert class , class b objects it.

my issue when attempt class b object in arraylist of class , cast class b follows

b foobar = (b)arraylistofclassa.get(indexofclassb). 

the value of bar in object b undefined.

how value of object b arraylist of class ???

don't try typecast instances this. check type before calling method / field using getclass()==.

if(myinstance.getclass()==b.class) //print myinstance.bar 

note : not design because breaking important thing generics introduced (and can see happens when that)


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -