what is the difference in using InputStream instead of FileInputStream while creating the FileInputStream object
Asked Answered
O

5

22

This may be a silly one, but I want to know the background operation difference.

  1. InputStream is = new FileInputStream(filepath);
  2. FileInputStream is = new FileInputStream(filepath);

What is the difference between the above two lines of code and in what scenarios are they used.

Oe answered 8/7, 2013 at 16:42 Comment(4)
FileInputStream is derived from InputStream - any FileInputStream instance is necessarily an InputStream. There's a broad preference for making declarations as abstract as possible.Burgher
There is no difference as you are doing both in both cases.Breton
Possible duplicate of Java - declaring from Interface type instead of ClassVibrato
This is also related: What does it mean to program to a interface?Vibrato
C
26

FileInputStream extends InputStream: it is a specialized version of an InputStream designed for reading files.

There are several implementations of an InputStream according to the use of it.

It is usually good practice to use the highest type needed in your code. Therefore if your code needs to read data from an InputStream but not specifically from a FileInputStream, you should use InputStream. Yet if you do need to keep the information of your object being a FileInputStream and not just an InputStream, then you should keep the FileInputStream type.

Constitutional answered 8/7, 2013 at 16:47 Comment(0)
I
13

There is no real difference. FileInputStream extends InputStream, and so you can assign an InputStream object to be a FileInputStream object. In the end, it's the same object, so the same operations will happen.

This behavior is called Polymorphism and is very important in Object-Oriented Programming.

Your first line of code is probably more desirable than the second as it doesn't lock you into a FileInputStream.

This is one of the strengths of object oriented programming. Not specifying a type allows you to change what type of stream you are using later on. If you are sure you'll only ever need a FileInputStream here, use the second line of code.

Impressure answered 8/7, 2013 at 16:44 Comment(0)
G
4

The other answers have pretty much nailed it, but I would like to add the following bit.

If the type of the reference variable is is strictly an internal implementation detail of your class, i.e. no other class will ever find out about it, directly or indirectly, then there is really no difference between the two statements, even though I would program against the more basic type (InputStream) just because.

However, if there is even the slightest hint of leaking FileInputStream specific behavior through your class' interface, without this being essential to the problem you are trying to solve, you should always program against the more basic type.

Of course, this is a general good practice and applies to more than InputStreams and the like.

Gemini answered 8/7, 2013 at 17:18 Comment(0)
E
3

Like the other answer state, there is no difference in behaviour. It still is the same object and the same methods will be executed. You can assign an object of any type that inherits InputStream to that variable.

However, what no one mentioned so far is: You can only call operations that are declared in InputStream on that variable. If FileInputStream would offer some additional operations, the compiler would throw an error if you try to call it. In this case you would need to use FileInputStream as type of the variable.

Ethelinda answered 8/7, 2013 at 16:54 Comment(0)
P
2

There is no difference. In each case you are creating a FileInputStream. The first is probably better programming style in that you should generally use a classes interface instead of the concrete class to allow for flexibility (i.e you decide to use a BufferedInputStream).

Philine answered 8/7, 2013 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.