Results 1 to 3 of 3
  1. #1
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default uses interface as type

    I'm just reading "Effective Java" from J. Bloch and I full agree with "uses interface as type". But, exactly with the example shown

    Java Code:
    // Good - uses interface as type
    List subscribers = new Vector();
    some useful methods inside the class Vector (ex. firstElement(), insertElementAt()) will not be visible in subscribers. Ok, subscribers could be casted to Vector, to access the method firstElement(). But, in the case you need methods of Vector is "use interface as type" still a good programming recommendation?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default Re: uses interface as type

    Quote Originally Posted by j2me64 View Post
    I'm just reading "Effective Java" from J. Bloch and I full agree with "uses interface as type". But, exactly with the example shown

    Java Code:
    // Good - uses interface as type
    List subscribers = new Vector();
    some useful methods inside the class Vector (ex. firstElement(), insertElementAt()) will not be visible in subscribers. Ok, subscribers could be casted to Vector, to access the method firstElement(). But, in the case you need methods of Vector is "use interface as type" still a good programming recommendation?
    Yes it is; as long as you only use the type (here: List) you're free to change the implementation (here: Vector) for another implementation, e.g. ArrayList. If you use the implementation type/class (here: Vector) your code is tied to the implementing type/class. As a corollary rule of thumb: use the least specific type that is still of use, so using a Vector as a type Serializable is too general and it is of (almost) no use.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: uses interface as type

    Quote Originally Posted by j2me64 View Post
    (S)ome useful methods inside the class Vector (ex. firstElement(), insertElementAt()) will not be visible in subscribers. Ok, subscribers could be casted to Vector, to access the method firstElement(). But, in the case you need methods of Vector is "use interface as type" still a good programming recommendation?
    Quote Originally Posted by JosAH View Post
    As a corollary rule of thumb: use the least specific type that is still of use, so using a Vector as a type Serializable is too general and it is of (almost) no use.

    kind regards,

    Jos
    The higlighted bit answers that question. If you need the methods in Vector then clearly List is too general a type to be using.

Similar Threads

  1. Replies: 4
    Last Post: 08-01-2011, 10:29 AM
  2. Interface return type invalid
    By Karenthian in forum New To Java
    Replies: 8
    Last Post: 05-09-2011, 11:58 PM
  3. Interface as a type!
    By guro in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 07:03 AM
  4. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  5. i can't understand using interface as a type
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 10:07 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
  •