开发者

What does this statement mean and ...?

开发者 https://www.devze.com 2023-03-23 19:54 出处:网络
Recently i have come across this statement : InputStream in = new FileInputStream(Filename开发者_开发问答);

Recently i have come across this statement :

InputStream in = new FileInputStream(Filename开发者_开发问答);

What does this statement mean ?

In this what does in refer to.?

Is in the object of FileInputStream ?

writing the statement : InputStream is = new InputStream(); produces an error b'coz InputStream is an abstract class but then why we have a constructor for this class ?--->InputStream()


This statement is leveraging the effects of inheritance. You are creating a new instance of a FileInputStream object and assigning it to the in variable.

As far as users of this variable go, they only see an object of the type InputStream - it could be any subclass of the abstract class InputStream. After this line, you can invoke any methods declared in the InputStream class on this object. Even though the object is really a FileInputStream, you can't see this, and therefore can't invoke those methods (without casting).

The constructor exists so that subclasses can call it to instantiate and set up any instance methods that all input streams need. You can't call it, but subclasses can invoke it.


Yes in is the name of an object of type InputStream (which is an abstract class) There could be many classes that extend that abstract class (including FileInputStream) - each of which implement the required components of InputStream.

You could create a specific object of the exact type of class you're using, but by using the abstract version or an interface - you're guarenteed the class has a certain set of functions but you can easily switch actual implementation without changing code. (Inheritance in action!)

For example the above line in the future could instead use:

InputStream in = new SocketInputStream();

For more information read the InputStream background.


in is the name of the variable whereas you're storing an instance of InputStream.


You are basically opening a file for reading.

in is the variable that you are assigning to the input stream. It is of class InputStream.


in is the variablename chosen here and it holds an object who's class is FileInputStream. The reason why the abstract class InputStream defines a constructor is so that it forces (non-abstract) subclasses to implement it.


  1. The identifier "in" refers to an InputStream object. In this case, it does refer to the FileInputStream object that was created by the constructor. That is possible because...

  2. InputStream is an abstract class. In Java, an abstract class can have defined methods (and constructors), however it can not be instantiated until it is extended. Subclasses that extend an abstract class must call the abstract class's constructor.

FileInputStream extends InputStream.


InputStream is an abstract class and so it cannot be directly instantiated. You can only assign an instance of a subclass of it which in this case a FileInputStream.

The next obvious question is why can't you assign it to a FileInputStream? Answer to that question would be to understand inheritance, abstract class and concepts of loose coupling. To be simple, tomorrow if you want to change your code such that instead of reading it from file, to read it say from an URL... You can do that without effecting rest of your code by just changing your FileInputStream to a URLInputStream(u have to write this one) which also extends the abstract InputStream class....


Here is an existing explanation for why abstract classes have constructors which may be relevant:

Why do abstract classes in Java have constructors?

Other than this addition I agree with Rudu's answer, in, is a local variable in our program that references an instance of the class FileInputStream. The fact that in the assignment it is only bound to a local variable of type InputStream means that within this scope it is treated as this type only by the compiler. Meaning as mentioned, you can only call methods that exist on the interface/ abstract class .. InputStream.

So while at runtime this will be an instance of type FileInputStream, because of java static type system and implementation of inheritance and assignments, it is at compile time within scope only usable as an InputStream.

0

精彩评论

暂无评论...
验证码 换一张
取 消