need for UPCAST


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: need for UPCAST

  1. #1
    Join Date
    Aug 2008
    Posts
    20

    Question need for UPCAST

    Dear Members :

    Could you please let me know the difference each of the following declarations will make ?

    Collection xList = new ArrayList(); ---- 1
    List xList = new ArrayList(); ---------- 2
    ArrayList xList = new ArrayList();------ 3

    I am ignoring the 1.5 way of declaration ie; ArrayList<Item> itemsList = new ArrayList<Item>();

    What I am trying to understand - what advantage/disadvantage I will get
    in declaring above ways (1-3) ?

    Thanks in advance.
    Atanu

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    3) is the normal declaration form of any object from any class:
    CLASS var=new CLASS(constructor_arguments);

    In number 1 & 2, u r just creating a more generalized object form, from its parents class, this object (xList in ur case) could be for example extended or changed latter in run-time even, for example this is correct :

    Collection xList = new ArrayList();
    xList = new AbstractCollection(); //OK
    xList = new Collection(); //OK

    but if u firstly declared it like that :
    ArrayList xList = new ArrayList();
    then this can't happens:
    xList = new Collection();

    so generally it depends on how u wanna use the created object, if it's JUST for an ArrayList use, it's better to use it as case #3
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Aug 2008
    Posts
    20

    need for UPCAST

    Hi Amahdy :

    Thanks very much.
    So can I conclude that if xList might behave in variety of ways (kind of simulating multiple inheritance) at run time; should we go for (1) or (2) ?

    Thanks
    Atanu

  4. #4
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    something like that, but not "multiple inheritance" exactly,
    this is just of benefit of the several childes, when u declare the object from the top parent :
    for example if u have Parent1 , with Child1, Child2, Child3
    then crating something like :
    Child1 x = new Child1(); //is very specific, u can use it for operations of Child1 ONLY

    but something like:
    Parent1 y = new Child1(); //will give u the chance to change it later "upcasting" to Child2 or Child3 ... since the object is originally a Parent1 .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  5. #5
    Join Date
    Aug 2008
    Posts
    20

    need for UPCAST

    Hi Amahdy :

    Thanks - appreciate your time.

    In last sentence you tried to mean upcasting Child2, Child3 to Parent1, if we do : y = new child2() or y = new child3() - right ? I am considering the hierarcy as Parent1->Child1->Child2->Child3 .

    Could you please tell few things about downcasting in this regard, I mean, when we need downcasting and will this work : Child3 c3 = new Child2() ?

    Thanks
    Atanu

  6. #6
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    no i meant :
    Parent1->Child1
    Parent1->Child2
    Parent1->Child3

    a small example that I needed and discussed here before in the c++ forum:

    I need to "encrypt" a data following the chosen encryption algorithm, there r many algorithms so I had to create an abstract parent class called encryption, and for each algorithm a class say "encryption1" "encryption2" "encryption3" ...etc
    now I have a coded object from "encryption", and depends on the user input the object will be reformatted depending on the chosen algorithm to the correct child ...

    another quick example is to create a "shape" and depening on what the user needs u reformat the class-object to "new circle" or "new rectangle" ..etc
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  7. #7
    Join Date
    Aug 2008
    Posts
    20

    need for UPCAST

    OK - Amahdy, thanks very much.

    Atanu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center