Results 1 to 7 of 7
  1. #1
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default create an object of interface

    It is said we cannot create an object of interface. What about the next code

    List myIntList = new LinkedList();

    Is List an interface?

    The code is on page 2 of http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

    Thanks.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    List is an interface, but you are not instantiating an object of List, you are instantiating an object of LinkedList.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It is an interface. List is not the actual object. It is the type. The reference type can be of any superclass or interface the class implements. You could also do
    Java Code:
    Object o = new String("Hi");
    You can also create an array of some interface type and fill it with implementing classes.

    It has something to do with polymorphism, but it gets a bit more complicated then that. I would try and google and see what you can find out about this topic.

  4. #4
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,757
    Rep Power
    7

    Default

    Yes List is an interface but the code does not create an instance of List, it creates an instance of LinkedList which is a concrete class.

    [mutter]
    stoopid phone call
    [/mutter]

  5. #5
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    Thank you all. Why does it not use
    LinkedList myIntList = new LinkedList();

    Is LinkedList myIntList = new LinkedList();
    more clear?

    Thanks again.

  6. #6
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,757
    Rep Power
    7

    Default

    It is generally accepted that you always use the interface type when declaring variables. Consider a method:
    Java Code:
    public void doStuff(LinkedList l) {
    
    }
    That restricts users to only be able to pass a LinkedList to the method.
    Java Code:
    public void doStuff(List l) {
    
    }
    This allows any List to be passed.

  7. #7
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

Similar Threads

  1. Create extra GUI interface
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 03-27-2011, 10:05 AM
  2. What is best object to create?
    By lam5442 in forum New To Java
    Replies: 1
    Last Post: 02-23-2011, 09:44 PM
  3. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  4. create object
    By paul21 in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 07:14 PM
  5. Create interface from my code
    By Lyricid in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 05:39 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
  •