Java: Set Interface Homework - Avoid Overriding Methods

  • Context: Comp Sci 
  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Interface Java Set
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
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