Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Tolls

Thread: Cross-Class Implementation

  1. #1
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default Cross-Class Implementation

    So, I have a situation where I have a set of abstract classes that have to interact with themselves and eachother, but only within a certain implementation. This results in a situation where I have something that looks like:

    Java Code:
    public interface Map
    {
         public Location getNearest(Location l);
    }
    Which I can implement with something that looks like:
    Java Code:
    public class 2DMap implements Map
    {
         public Location getNearest(Location l)
         {
               //actual code goes here
          }
    }
    Or something that looks like:
    Java Code:
    public class 2DMap implements Map
    {
        public 2DLocation getNearest(2DLocation l){
              //actual code goes here
        }
    }
    In the first option, the 2DMap would be expected to handle Locations from other implementations- something from a 3D map or a hex grid, which it clearly could not do. The second option, however, does not compile, because it doesn't really fufill the method header.

    This is something I could fix easily enough with casting- within each method cast the relevant types to what they need to be- but it feels like there should be a more elegant, OOP-friendly way do to so.

    What would be the "correct" way to go about this? Mind, not just looking for specific answers to the example listed, but for this type of situation in general- abstract systems where the specific implementations work in the described way, but only within the implementation.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Cross-Class Implementation

    Generics?
    Map<T extends Location>?
    (Note syntax is suspect as I rarely write these things).
    quad64bit likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Abstract class, implementation, getName()?
    By manalinik in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 02:08 PM
  2. Replies: 0
    Last Post: 12-08-2011, 12:43 PM
  3. Class within one class implementation
    By gomdohri in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 02:05 AM
  4. Implementation Class for RMI App, Compile-Time Error
    By Jacob1028555 in forum Networking
    Replies: 22
    Last Post: 06-09-2011, 05:14 PM
  5. Help with implementation of the DataSet class
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:47 AM

Tags for this Thread

Posting Permissions

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