Results 1 to 7 of 7
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default ArrayList of interface, does it rip away data from child classes?

    If I lets say have an interface Animal, and I create a lot of classes with a different animal name that implement the interface Animal. Then I create an ArrayList of Animal. Then I would put in lets say Dog class into the ArrayList, which has custom methods and data that the Animal Interface doesn't have, is this data ripped away except for the methods that are put in the Animal interface? So if I would cast the Animal back to Dog, would it retain all the data that existed before it was placed in the ArrayList?
    Regards
    augustas656

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: ArrayList of interface, does it rip away data from child classes?

    For class objects, nothing is 'ripped away'; Java is not like C++ that does slicing; of course you may need to cast the type of object in your ArrayList to its actual type, because all Java knows is that the object is an Animal ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: ArrayList of interface, does it rip away data from child classes?

    I was just wondering, I thought casting removes unnecesary data from the child class, and I thought placing child classes in an arraylist of a super class does the same.
    Regards
    augustas656

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: ArrayList of interface, does it rip away data from child classes?

    Think about it this way. If you have a class Dog that subclasses Animal you can cast Dog to Animal. Since the Dog object is now being referred to as an animal, only the Animal methods are accessible. Recasting the Animal back to Dog makes those methods accessible (visible) again. But when you cast an interface or superclass to an implementation or subclass you need to make certain the cast is allowable. Using instanceof solves this problem.

    Also, if the original cast removed unnecessary data, then the data could not be restored when you recasted the superclass to the subclass. So how could it ever work?

    Edit: Clearly it does work since according to Jos, C++ does something similar. But I fail to see the advantages.

    Regards,
    Jim
    Last edited by jim829; 11-29-2014 at 06:34 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: ArrayList of interface, does it rip away data from child classes?

    I didn't really know, I wanted to find out if casting removes class data and methods that don't exist in the class that is being casted to, or how I understand now. Thanks!
    Regards
    augustas656

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: ArrayList of interface, does it rip away data from child classes?

    Quote Originally Posted by jim829 View Post
    Edit: Clearly it does work since according to Jos, C++ does something similar. But I fail to see the advantages.
    It isn't considered an advantage in C++ either; slicing is considered an ugly bug because it severely ruins your object by chopping off the last part of it ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: ArrayList of interface, does it rip away data from child classes?

    Quote Originally Posted by JosAH View Post
    ... it severely ruins your object by chopping off the last part of it ...

    kind regards,

    Jos
    Ouch!
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 12
    Last Post: 11-07-2013, 03:42 AM
  2. Why both ArrayList and AbstractList are implements List interface?
    By muthukrishnan.muthu in forum New To Java
    Replies: 2
    Last Post: 02-28-2013, 05:36 PM
  3. Replies: 5
    Last Post: 10-31-2012, 06:34 PM
  4. interface vs abstract classes
    By chandrasekhar.melam in forum New To Java
    Replies: 1
    Last Post: 10-05-2011, 03:46 PM
  5. interface vs abstract classes
    By rosh72851 in forum New To Java
    Replies: 7
    Last Post: 11-16-2008, 09:22 PM

Posting Permissions

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