Comp Sci Java: Set Interface Homework - Avoid Overriding Methods

Click For Summary
To avoid overriding all methods from the Set interface when implementing a custom set class, one option is to extend an existing Set implementation like HashSet or EnumSet, which provides the necessary method implementations. Alternatively, making the custom class abstract can allow you to leave some methods unimplemented, but this may not be the most efficient approach. Using a backing data structure, such as an ArrayList, is not recommended since ArrayList does not enforce set behavior, allowing duplicates. The discussion emphasizes the importance of adhering to the Set interface's contract while implementing additional functionality. Ultimately, leveraging existing Set implementations is the most straightforward solution.
Robben
Messages
166
Reaction score
2

Homework Statement



If I have a subclass that implements a set class how can I avoid having to override all the set classes methods?

Homework Equations



None

The Attempt at a Solution



I have an interface:

Java:
import java.util.Set;

    /**
     * An extended Set where there is added functionality
     *
     * @version 3/23/14
    */
    public interface NewSet<T> extends Set<T> {
        /**
         * Calculates the set union with the given input
         *
         * @param set - the set to perform the operation along with
         * @return  the set representing the invoked set and the parameter
        */
         NewSet<T> union(NewSet<T> set);

         //and a bunch more methods ...
    }

And I have a class that extends this interface:

Java:
    import java.util.ArrayList;
    public class GSet<T> implements NewSet<T> {
        // ...
    }

Now, all my methods in NewSet has been implemented into GSet, but since NewSet extends Set<T> the compiler wants me to implement the methods of Set, i.e. addAll(), removeAll(), containsAll(), and more but I don't want to override those methods. Is there away to avoid this? Do I make my GSet abstract and thus make the methods that need to be implemented from set abstract as well? Or is there an easier way to go about doing this like backing up my GSet class with an arraylist?
 
Physics news on Phys.org
jedishrfu beat me to it.

If your assignment requires that you build it the way that you've shown, you can look at the HashSet source to quickly put together the implementations.
 
Is it okay if I extend ArrayList instead because we haven't discussed HashSet yet?
 
  • Like
Likes Robben

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K