Abstract class with inheritance
I have an Abstract class ADTClass, and another class BasicDT that extends ADTClass. The ADTclass has 2 fields
field 1 of type Object
field 2 of type Collection
now in my BasicDT class i have a constructor:
now I assume that when i use field 2 now in methods, field 2 would be of type ArrayList and i could use methods such as get or add which are methods built in the arraylist datatype. However field 2 stays of type collection and i cant use some of the methods in particular "get()".
field 1 = new Object();
field 2 = new ArrayList();
Please could someone tell me where im going wrong or how i can change field 2 to type ArrayList without changing it in the ADTClass and without giving new fields in the BasicDT Class
Any Help Very much appreciated.
Re: Abstract class with inheritance
You have defined field2 as a Collection, so that is what you get - even though you may have instantiated it as an ArrayList, defining the variable as a Collection will limit you to the methods Collection has to offer. You can redefine it as a List, cast to a List, or any number of other techniques to get your desired result but you should think hard about the code and inheritance design and make sure this is a proper design for your requirements.