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.
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!!
public interface SimpleSet
//returns a new set (a new object) containing the same set of elements as this set
Here is what i have done till now:
Appreciate any help.
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 !!
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.
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 ?
create a method that does it for you.
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.