Results 1 to 12 of 12
Thread: Defining the inputStream object
- 06-17-2010, 03:11 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
Defining the inputStream object
Question regarding extend issue
What is the reason behind defining the inputStream object in this way...
I know that FileInputStream extends InputStream, is there anything else ?Java Code:InputStream inputStream = new FileInputStream("c:\\input.txt");
I mean we could have define it like
Thank youJava Code:FileInputStream inputStream = new FileInputStream("c:\\input.txt");
- 06-17-2010, 03:42 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
If you don't need functionality from the FileInputStream, that is there's no specific methods required, then simply using InputStream is viewed as best practice, since you know that changing the right hand side to new SomeOtherInputStream() won't affect the rest of the code.
For the same reason, methods should generally return an interface (ie List rather than ArrayList) so it allows you to change the specific class if you feel ArrayList isn't performing well enough...
That's probably a rubbish explanation, so I apologise in advance!
:)
- 06-17-2010, 03:50 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
It's not rubbish at all; there's the old mantra "code to the interface, not to the implementation" and the same holds for classes: always treat an object as the most abstract class (i.e. a class more to the root (Object)) that still fullfills your needs. A FileInputStream is an InputStream and most of the time you want to treat it that way; you can't treat is an Object because you can't read from an Object, nor can you close it etc. The entire I/O framework was designed as a bunch of wrappers and you can treat the classes in the framework as such.
kind regards,
Jos
- 06-17-2010, 03:52 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
It's an example of coding to the interface rather than to an implementation which enhances code reuse.
In this example you can reuse the inputStream variable in later parts of the code for other InputStreams that are not FileInputStreams.
A more useful example is in parameter types for methods.
If a method needs, say a List, and is able to perform its operations by using only the methods declared in the List interface, then it is better to declare it's argument as List rather than, say, ArrayList because that allows you (and other people) to use it with any implementations of List without rewriting specific methods for each List implementation.
- 06-17-2010, 03:54 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Give me back my faster typing fingers Jos.
I promise that I won't blow the vuvuzela again. I'll even find time to review the code for ... <fingers too slow to finish this sentence in time>
- 06-17-2010, 03:56 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
- 06-17-2010, 03:56 PM #7
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
Thank you for Tolls JosAH for the helpful replys...
Really helped ...
- 06-17-2010, 04:05 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 06-17-2010, 04:05 PM #9
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
Dear r035198x
but I can still reuse the variables of both FileInputStreams and InputStreams if I created the object in usual method, right ?In this example you can reuse the inputStream variable in later parts of the code for other InputStreams that are not FileInputStreams
Thank you
- 06-17-2010, 04:07 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
- 06-17-2010, 04:09 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
The first person who is going to honk on a vuvuzela in my vicinity again is going to get the thing stuffed deep in his/her throat.
b.t.w. I'm going to upload yet another version of the stuff; nothing special, just bug fixes and a few enhancements in the expression manipulation stuff.
kind regards,
Jos
- 06-17-2010, 04:10 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Creating an InputStream
By c26354 in forum New To JavaReplies: 10Last Post: 07-12-2011, 04:48 AM -
Java InputStream
By Bill88 in forum New To JavaReplies: 10Last Post: 09-21-2009, 02:40 PM -
defining own exception
By javaplus in forum New To JavaReplies: 2Last Post: 11-12-2007, 12:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks