Results 1 to 8 of 8
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default Quick LinkedList question

    It seems that this is a proper way to declate and create a LinkedList
    Java Code:
    	private LinkedList<String> linkedList = new LinkedList<String>();
    Why the following declaration does not allow me to get a full access to LinkedList methods?

    Java Code:
    	private List<String> linkedList = new LinkedList<String>();
    Is be because that List Interface is too generic?

    If so, why does the following declaration fails to give me access to LinkedList specific methods as well?

    Java Code:
    	private AbstractSequentialList<String> linkedList1 = new LinkedList<String>();
    The way i understand it is that, the relationship is:

    List --> AbstractList --> AbstractSequentialList --> Linked List

    Please help me understand this

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    You need to look at how inheritance works. The variable in
    List<String> linkedList
    is type List not type LinkedList. It will only have the methods for List.
    Because a LinkedList is a List, you can assign a LinkedList object to a Link variable.

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    From what i understand we choose between data structures based on the methods said data structures make available for us. Given a choise between an ArrayList or LinkedList, i would look at the problem i am trying to solve and chose either.

    I have been criticized before for declaring variables as :
    Java Code:
    ArrayList<Object> myList = new ArrayList<Object>();
    and was told that when class is chosen to represent object's type, it limits my code to (in this case to) ArrayList alone, where as declaration below:

    Java Code:
    List<Object> myList = new ArrayList<Object>();
    Affords me a possibility to later redefine myList as, say LinkedList.

    But why would i do that? Only if indeed LinkedList offers a better solution to what i am trying to solve and has methods available for me to use. In the above declaration, however, i am not able to access methods specific to LinkedList, hence again a question:

    Going forward where should i draw the line while declaring object to Interface vs Implementation?

    Thx

  4. #4
    falven is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    You would do that because In the future, you could cast your List type to a Linked list type to access it's variables, whereas if you had a LinkedList type you wouldn't be able to cast it to let's say an ArrayList if you wanted to access it's methods.

  5. #5
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    So, from good programming practices standpoint, this actually is a right way of doing things, if need to get access to removeFirst() method?

    Java Code:
    private List<String> linkedList = new LinkedList<String>();
    
    ((LinkedList<String>) linkedList).removeFirst();

  6. #6
    falven is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    Well I think you would have to actually create a casted variable to access the methods, but yeah, this is the correct way of doing it.
    And really, it's not generally a big deal unless you're writing programs for like a big corporation or an open source community or the such.
    good luck,
    -Falven

  7. #7
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Ok, cool. Thanks Falven

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Declaring variables as the superclass or interface is generally good practice, but can be extraneous at times. My rule of thumb is, if I'm using the list as a method argument or return type, try to make it as abstract as possible, but if I'm using it just as a helper, I don't really bother. Something like this:
    Java Code:
    public class Example {
      public List<String> getList() {}
      public void someListMethod(List<Integer> list) {}
      public void readFromFile(String path) {
         //open the file with some reader
         ArrayList<String> arr = new ArrayList<String>();
      }
    }
    The arraylist in the readFromFile method is not visible to the outside, it doesn't get returned, and generally, the class that would use the method doesn't care how the file gets read, so in this case, I declare the variable as ArrayList instead of List. The methods that take a List as argument or return a List should be abstracted, so when you change a little bit of your code, the methods still work. The someListMethod() will work with ArrayLists, Vectors and anything else that implements List. Have a read through this article, although the author does go a bit overboard, most of his points are valid, and good programming practice.
    Why extends is evil - JavaWorld
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Really quick question
    By shadycharacter in forum New To Java
    Replies: 2
    Last Post: 04-22-2010, 10:06 PM
  2. Quick question
    By sAntA199 in forum New To Java
    Replies: 2
    Last Post: 12-09-2009, 03:01 AM
  3. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:53 AM
  4. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 PM

Posting Permissions

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