Results 1 to 6 of 6

Thread: interface

  1. #1
    SuperPig is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default interface

    List<String> list = new ArrayList<String>(c);

    As we know, interface is an abstract class thus it cant be instantiated.
    As I know, ArrayList is a concrete class which can be instantiated.
    Can someone please tell me why "List<String> list = new ArrayList<String>(c);" is valid..~
    In this circumstance, why List(interface) can hold an object?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by SuperPig View Post
    List<String> list = new ArrayList<String>(c);

    As we know, interface is an abstract class thus it cant be instantiated.
    Yes and no. No, interface is not an abstract class which is a different animal, and yes, both interface and abstract classes cannot be instantiated.

    As I know, ArrayList is a concrete class which can be instantiated.
    correct

    Can someone please tell me why "List<String> list = new ArrayList<String>(c);" is valid..~
    In this circumstance, why List(interface) can hold an object?
    An interface is a pure type and while you cannot create a List object you certainly can create a List variable that refers to a concrete object (here ArrayList) that implements the List interface. So the key here is to understand that the line of code is not trying to instantiate a List but an ArrayList, and then place that ArrayList in a List variable. We use interface variables all the time, and they are a powerful concept since they allow you to give your code much flexibility. If later you find that the code runs more efficiently if use use a LinkedList rather than an ArrayList, it's easy to change the code so that the object referred to by the List variable is a LinkedList object, and this can be done with minimal change in existing code.

  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

    Edit: too slow :(

    You aren't instantiating an interface in this example, you are creating a reference of that type, but the reference points to a concrete class(ArrayList), since this reference is a list, you can only call methods which the list class has. Through polymorphism the list methods work properly in this situation.

    This convention allows for a lot of flexibility. Say you develop a program with data structure X which implements list and subsequently create a faster(or threadsafe, etc), data structure y which also implements list you can easily change the reference to point to y without breaking previous code.

    Always remember that the reference is not actually be instantiated, instead the rvalue is.

    For more on polymorphism, read here: Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

  4. #4
    SuperPig is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    ok got it..~ We can have a List variable which can hold an object of ArrayList... Can i have "Abstract class" variable as well to hold an object?

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

    Default

    Yes, since you aren't actually instantiating the abstract class.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by SuperPig View Post
    ok got it..~ We can have a List variable which can hold an object of ArrayList... Can i have "Abstract class" variable as well to hold an object?
    Why not test this yourself with your own in-home lab: your Java compiler?

Similar Threads

  1. interface
    By makrandubale in forum New To Java
    Replies: 9
    Last Post: 01-02-2011, 03:13 PM
  2. How do I use an Interface for this?
    By aaronfsimons in forum New To Java
    Replies: 14
    Last Post: 06-22-2009, 02:19 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
  •