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

后端 未结 5 1011
天涯浪人
天涯浪人 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: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.

提交回复
热议问题