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
FileInputStreamobject and assigning it to theinvariable.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 classInputStream. After this line, you can invoke any methods declared in theInputStreamclass on this object. Even though the object is really aFileInputStream, 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.