24

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.

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. Commented Jul 8, 2013 at 16:46
  • There is no difference as you are doing both in both cases. Commented Jul 9, 2013 at 0:39
  • 1
    Possible duplicate of Java - declaring from Interface type instead of Class Commented Mar 29, 2016 at 12:24
  • This is also related: What does it mean to program to a interface? Commented Mar 29, 2016 at 12:24

5 Answers 5

28

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

5

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.

Comments

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.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.