Results 1 to 3 of 3
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Declaring a collection with Interface name vs. that with implementing Class' name

    Note that the code always refers to the Collection by its interface type (Set) rather than by its implementation type. This is a strongly recommended programming practice because it gives you the flexibility to change implementations merely by changing the constructor.
    - source

    Assuming that 'changing implementations' means something like this:

    Java Code:
    Set<Integer> hashSet = new HashSet<>();
    hashSet.add(1);
    hashSet.add(2);
    hashSet.add(3);
    
    List<Integer> arrayList = new ArrayList<>(hashSet);
    But then continuing from previous quote,
    If either of the variables used to store a collection or the parameters used to pass it around are declared to be of the Collection's implementation type rather than its interface type, all such variables and parameters must be changed in order to change its implementation type.
    But I was also able to do declare hashSet with the implementation (HashSet), rather than the interface (Set):

    Java Code:
    HashSet<Integer> hashSet = new HashSet<>();
    hashSet.add(1);
    hashSet.add(2);
    hashSet.add(3);
    
    List<Integer> arrayList = new ArrayList<>(hashSet);
    I was able to do it without any problems, and it worked just like the previous version. The question is why? Am I not understanding what they are saying correctly?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Declaring a collection with Interface name vs. that with implementing Class' name

    What it means is:
    Java Code:
    public class MyClass {
        Set<Integer> mySet = new HashSet<>();
    
        public Set<Integer> getMySet() {
            return mySet;
        }
    }
    In a class using the above:
    Java Code:
    Set<Integer> theSet = myObject.getMySet();
    Now, if I decide (for whatever reason) that a TreeSet makes more sense in MyClass I can change that without having to change any of the code that uses it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Declaring a collection with Interface name vs. that with implementing Class' name

    As an addendum to what Toll's said, sometimes you may need additional functionality
    that is included in the implementation that is not in the interface. In those cases you
    would use the implementation as the type. An example is the ListModel interface. Imho,
    it is rather anemic in terms of functionality. If possible, I mostly use the DefaultListModel.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Need Urgent Help in Implementing Interface Class
    By yel_hiei in forum New To Java
    Replies: 4
    Last Post: 07-29-2010, 01:42 PM
  2. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 03:33 PM
  3. help! implementing an interface
    By manda147 in forum New To Java
    Replies: 28
    Last Post: 11-17-2008, 04:27 AM
  4. Implementing an interface
    By bugger in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2008, 01:35 PM
  5. Declaring Interface
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •