Results 1 to 9 of 9
  1. #1
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default implement a type of container

    Read this question as a one of very basic java level.
    I want to implement a kind of "container" in which to store objects (instances) of different types. Then with an iterator I'd call common methods. This is what I have in mind:
    Java Code:
    with(Positionables){
        translate(2, 0, 4);
        }
    where translate(x, y, z) is a method common for objects in Positionables which objects are of different types (Sphere, Box etc.).

    Now I was thinking Positionables could be a List<Positionable> and Positionable is an abstract class and Sphere and Box extends from it. But I don't know how to propagate the call of translate() to the subclasses.

    What are the best approaches for this matter? It would be perfect if I could make it so I could somehow use the "with" construction like in the example above.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: implement a type of container

    Is the 'enhanced for-loop' of any use? e.g.

    Java Code:
    for (Positionable p : myPostionables)
       p.translate(2, 0, 4);
    where 'myPositionables' is a container (i.e. Iterable) over the Postionable(s).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: implement a type of container

    Thank you for the prompt reply. It looks good but how do I put the different type objects in myPositionables? Is this a class or a Set/List? In the former case, does the call propagate to child types? I'm a little confused. I'm a java beginner :)
    EDIT:
    I'm sorry, I'm still fixing some java things in my mind. I now see how to do it:
    Java Code:
    List<Positionable> myPositionables = new ArrayList<Positionable>();
    myPositionables.add(sphere);
    myPositionables.add(box);
    where sphere and box are of their respective types -- subclasses of Positionable.

    But I suppose I can't wrap this code into a simpler form like the "with(Positionables)", I'm just asking...
    Last edited by mireazma; 03-15-2014 at 03:24 PM.

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

    Default Re: implement a type of container

    If Positionable is a totally abstract class with no embedded fields then I would recommend using interfaces. You can implement multiple interfaces for a given class but you can only extend a single class. You would still add them to the list the same way.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: implement a type of container

    It has to be a class, as I want the different subclasses to have x, y, z coordinates etc. I could never understand why Java rejected multiple inheritance. I've read that it was for avoiding the confusion with conflicting attributes; but as conflicting methods are treated, so should the attributes: a simple enforcement like qualified naming would do.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: implement a type of container

    Quote Originally Posted by mireazma View Post
    It has to be a class, as I want the different subclasses to have x, y, z coordinates etc. I could never understand why Java rejected multiple inheritance. I've read that it was for avoiding the confusion with conflicting attributes; but as conflicting methods are treated, so should the attributes: a simple enforcement like qualified naming would do.
    Java doesn't implement multiple inheritance (MI) of implementation and if you've ever seen the horrible fiddling with the 'this' pointer (as C++ has to do) or if you've ever seen accidental slicing of objects (as most inexperienced programmers that used MI of implementation have witnessed) you don't want MI of implementation; never.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: implement a type of container

    Eh hey, if only they do the same in the physical world, with technology... like to forbid nuclear power altogether as we've seen what barely imaginable things it brought about... but it's human nature: man commits acts out of capability without a purpose, regardless of consequences and he considers himself rational :))

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: implement a type of container

    Quote Originally Posted by mireazma View Post
    I could never understand why Java rejected multiple inheritance.
    Java is a programming language and a programming platform; it has no capability to reject anything. People did, with good reason.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: implement a type of container

    Quote Originally Posted by gimbal2 View Post
    Java is a programming language and a programming platform; it has no capability to reject anything. People did, with good reason.
    I can't wait to get to the level of proficiency to understand the reasons (no sarcasm!).
    And BTW, you're saying: Constitution doesn't allow or prohibit anything; people who signed it do ;)

Similar Threads

  1. Replies: 2
    Last Post: 12-21-2012, 05:32 AM
  2. Replies: 6
    Last Post: 09-14-2012, 01:33 PM
  3. Replies: 4
    Last Post: 08-01-2011, 11:29 AM
  4. Help with adding a Container within a Container
    By JoKeR313 in forum New To Java
    Replies: 4
    Last Post: 01-02-2011, 05:59 AM
  5. Replies: 3
    Last Post: 04-22-2009, 12:01 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
  •