Results 1 to 3 of 3
Thread: uses interface as type
- 09-19-2011, 11:51 AM #1
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
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?Java Code:// Good - uses interface as type List subscribers = new Vector();
- 09-19-2011, 12:15 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: uses interface as type
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-19-2011, 02:31 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Similar Threads
-
Trying to convert Java Date to type int to fit in mysql table with field type int(11)
By fortwnty420 in forum New To JavaReplies: 4Last Post: 08-01-2011, 10:29 AM -
Interface return type invalid
By Karenthian in forum New To JavaReplies: 8Last Post: 05-09-2011, 11:58 PM -
Interface as a type!
By guro in forum New To JavaReplies: 4Last Post: 03-10-2009, 07:03 AM -
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
i can't understand using interface as a type
By sireesha in forum New To JavaReplies: 3Last Post: 11-20-2007, 10:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks