Results 1 to 5 of 5
  1. #1
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default Regarding interface

    Hey,
    Interfaces don't have constructors or objects but this one is bothering me a lot.
    Here is the scenario:
    There's an interface "SimpleSet" with a method called "copyOf()" that returns a SimpleSet object, i.e.
    Java Code:
    public interface SimpleSet
    {
        //returns a new set (a new object) containing the same set of elements as this set
    	SimpleSet copyOf();
    }
    Now, I have implemented the interface using class HashSetImpl. The problem is that when i create an object of HashSetImpl it also represents the SimpleSet but i do not know how to copy another hash set's element into a new hash set that is represented by the HashSetImpl (SimpleSet) object. Creating an object of a class inside the class just doesn't make sense!!
    Here is what i have done till now:
    Java Code:
    public class HashSetImpl implements SimpleSet 
    {
      HashSetImpl hsi = new HashSetImpl();
      HashSet hs = new HashSet();//older hash set that contains some elements
      HashSet hsn = new HashSet();//new hash set 
    
      public SimpleSet copyOf()
    	{
    	     hsi.hsn.add(hs);//didn't know what else to do !!
    	     return hsi;
            }
    }
    Appreciate any help.
    Last edited by hedonist; 10-13-2009 at 02:56 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You don't need three sets in there.
    You just create the new one inside the copyOf method and fill it with values from the current set.

  3. #3
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default

    Thanks, but how do i link an object of HashSetImpl with a new hash set so that i can copy the value from old hash set to the new hash set and return SimpleSet from "copyOf" method ?

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    create a method that does it for you.
    I die a little on the inside...
    Every time I get shot.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What does your HashSetImpl class look like? Does it contain an instance of a HashSet?
    If so, then your copy method instantiates a new HashSet, copies values from the instance hash set to this newly created one and returns the newly created one.
    It doesn't get any simpler than that.

Similar Threads

  1. How do I use an Interface for this?
    By aaronfsimons in forum New To Java
    Replies: 14
    Last Post: 06-22-2009, 02:19 PM
  2. Interface?
    By makpandian in forum New To Java
    Replies: 5
    Last Post: 03-26-2009, 10:59 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
  •