21

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.

FIFA oneterahertz
  • 718
  • 2
  • 16
  • 43
Mathan Kumar
  • 634
  • 2
  • 7
  • 19
  • FileInputStream is derived from InputStream - any FileInputStream instance is necessarily an InputStream. There's a broad preference for making declarations as abstract as possible. – user888379 Jul 08 '13 at 16:46
  • There is no difference as you are doing both in both cases. – user207421 Jul 09 '13 at 00:39
  • 1
    Possible duplicate of [Java - declaring from Interface type instead of Class](http://stackoverflow.com/questions/3383726/java-declaring-from-interface-type-instead-of-class) – Tom Mar 29 '16 at 12:24
  • This is also related: [What does it mean to program to a interface?](http://stackoverflow.com/questions/1413543/what-does-it-mean-to-program-to-a-interface) – Tom Mar 29 '16 at 12:24

5 Answers5

24

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.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
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.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
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.

dkateros
  • 1,574
  • 10
  • 14
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.

André Stannek
  • 7,773
  • 31
  • 52
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).

John B
  • 32,493
  • 6
  • 77
  • 98