1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Java: Set Interface

  1. Apr 9, 2015 #1
    1. The problem statement, all variables and given/known data

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

    2. Relevant equations


    3. The attempt at a solution

    I have an interface:

    Code (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:

    Code (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?
  2. jcsd
  3. Apr 9, 2015 #2


    Staff: Mentor

  4. Apr 9, 2015 #3


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
  5. Apr 9, 2015 #4
    Is it okay if I extend ArrayList instead because we haven't discussed HashSet yet?
  6. Apr 9, 2015 #5


    Staff: Mentor

    I don't think ArrayList is a Set though as it can allow multiple instances of the same object.

    ArrayList is a cloneable, serializable and randomaccess List


    You need to use one of the follow to subclass:
    Set setA = new EnumSet();
    Set setB = new HashSet();
    Set setC = new LinkedHashSet();
    Set setD = new TreeSet();

    Others are shown here:

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted