Results 1 to 2 of 2
  1. #1
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default 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:

    Java Code:
    public BasicDT(){
    field 1 = new Object();
    field 2 = new ArrayList();
    }
    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()".
    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.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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.

Similar Threads

  1. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 06:58 PM
  2. Replies: 4
    Last Post: 04-14-2011, 11:42 AM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  4. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •