Java: Set Interface Homework - Avoid Overriding Methods

In summary, the conversation discusses how to avoid overriding all set classes methods when implementing a subclass that implements a set class. It is suggested to extend from an established Set collection class such as HashSet or EnumSet. If the assignment requires a different approach, one can look at the HashSet source to quickly put together the implementations. Another suggestion is to use one of the available subclasses of Set, such as LinkedHashSet or TreeSet. It is noted that ArrayList is not a Set and cannot be used for this purpose.
  • #1
Robben
166
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
  • #3
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.
 
  • #4
Is it okay if I extend ArrayList instead because we haven't discussed HashSet yet?
 
  • #5
  • Like
Likes Robben

Related to Java: Set Interface Homework - Avoid Overriding Methods

What is the Set Interface in Java?

The Set Interface in Java is a collection interface that extends the Collection interface and represents an unordered collection of unique elements. It does not allow duplicate elements and provides methods for adding, removing, and searching for elements in the set.

What is the purpose of the Set Interface?

The purpose of the Set Interface in Java is to provide a way to store and manipulate a group of unique objects without worrying about their order or duplicates. It is useful for tasks such as removing duplicates from a collection, checking for membership of an element, and performing set operations like union and intersection.

Why should I avoid overriding methods in the Set Interface?

Overriding methods in the Set Interface can lead to unexpected behavior and can potentially break the contract of the interface. Since the Set Interface already provides methods for adding and removing elements, overriding them can cause issues with the implementation of the interface and can also affect other classes that use the Set Interface.

What are some alternatives to overriding methods in the Set Interface?

One alternative to overriding methods in the Set Interface is to use a different data structure that allows for custom behavior, such as a custom implementation of the Set Interface using a HashMap. Another option is to use the existing methods and perform any necessary transformations or operations on the set before or after using them.

Is it ever appropriate to override methods in the Set Interface?

In general, it is not recommended to override methods in the Set Interface. However, there may be cases where it is necessary, such as when creating a custom implementation of the Set Interface that requires different behavior. In these cases, it is important to carefully consider the implications of overriding methods and to thoroughly test the implementation to ensure it does not break the interface's contract.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
Back
Top