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

后端 未结 5 972
天涯浪人
天涯浪人 2021-02-02 13:54

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

  1. InputStream is = new FileInputStream(filepath);
  2. FileIn
相关标签:
5条回答
  • 2021-02-02 14:19

    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.

    0 讨论(0)
  • 2021-02-02 14:19

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

    0 讨论(0)
  • 2021-02-02 14:25

    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.

    0 讨论(0)
  • 2021-02-02 14:26

    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.

    0 讨论(0)
  • 2021-02-02 14:27

    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.

    0 讨论(0)
提交回复
热议问题