Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference in Declarations  RSS feed

 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between
FileInputStream f = new FileInputStream();
and
InputStream f = new FileInputStream(); ?
What are the advantages of using the second form of declaration?
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob Thomas:
What is the difference between
FileInputStream f = new FileInputStream();
and
InputStream f = new FileInputStream(); ?
What are the advantages of using the second form of declaration?


in this context you wont see any advantages, and considering FileInputStream provides methods such as getChannel(), you may even consider the second form to deprive you of methods, but lets say you have a method called readStream(SomeInputStream stream) , and you dont need to getChannel(), then it would make perfect sense to have your method take an InputStream as a parameter and you wont care if you're assigning your InputStream object to a FileInputStream or a PipedInputStream since they both extend from InputStream and provide the same services such as read()
now what you have is instead of say

and be limited to reading only FileInputStream's you can now have

and read from any InputStream.
please excuse the cheesy example I but I couldn't think of another way to explain it
 
Gayathri Prasad
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The relation between the Inputstream and File InputStream is like the following...
java.lang.Object
|
+--java.io.InputStream
|
+--java.io.FileInputStream
and more so all the known InputStream classes like (AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream) are sub-classes of the abstract class InputStream .
The following two methods are specific to the FileInputStream class and NOT available to InputStreamClass
FileChannel getChannel()
Returns the unique FileChannel object associated with this file input stream.
FileDescriptor getFD()
Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
ATB,
Gaya3
-------------------------------------------------
Beginning is half done.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!