Java: Set Interface Homework - Avoid Overriding Methods

  • Context: Comp Sci 
  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Interface Java Set
Click For Summary

Discussion Overview

The discussion revolves around a homework problem regarding the implementation of a custom set class in Java that extends the Set interface. Participants explore how to avoid overriding all methods from the Set interface while implementing additional functionality.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster seeks a way to avoid implementing all methods from the Set interface in their custom class GSet, which extends a new interface NewSet.
  • Some participants suggest extending from established Set collection classes like HashSet or EnumSet as a potential solution.
  • One participant mentions that if the assignment requires the current structure, examining the source code of HashSet could help in implementing the required methods.
  • A question is raised about the possibility of extending ArrayList instead, although this is met with skepticism regarding its suitability as a Set implementation.
  • Another participant clarifies that ArrayList allows duplicates and does not conform to the Set interface, suggesting specific Set implementations that could be used instead.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to implement the custom set class. There is no consensus on whether extending ArrayList is appropriate, and the discussion remains unresolved regarding the best method to avoid overriding Set methods.

Contextual Notes

Participants note the limitations of using ArrayList in this context due to its nature as a List rather than a Set. The discussion also highlights the need to adhere to the requirements of the assignment while considering the implementation details of existing Set classes.

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   Reactions: 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 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K