0

I have small doubt, this could be very lame question but please bear with me.

I was checking the source code of FileReader. So when ever a FilerReader created it constructs FileInputStream. Here is the code snippet

public class FileReader extends InputStreamReader {


    public  FileReader(String fileName) throws FileNotFoundException {

        super(new FileInputStream(fileName));

    }

My Doubt here is FileReader extends InputStreamReader, here FileReader is a child of InputStreamReader, but when constructor of FileReader is called, then how come this is possible "super(new FileInputStream(fileName));" ?

There is no constructor or method which accepts new FileInputStream(fileName) as parameter in parent class which is InputStreamReader.

Fileinput stream has following hierarchy ..

 java.lang.Object
    java.io.InputStream
        java.io.FileInputStream

Doesnt seems to be any relation between FileInputStream and InputStreamReader..!

Can anyone help me to understand this ? How and from where super(new FileInputStream(fileName)); is being called here ?

Andrea
  • 109
  • 2
  • 10

1 Answers1

1

There is no constructor or method which accepts new FileInputStream(fileName) as parameter in parent class which is InputStreamReader.

True, but FileInputStream is a subclass of InputStream, so that FileReader constructor is calling InputStreamReader​(InputStream in). Remember, you can pass a subclass instance (FileInputStream object) in as an argument to a method or constructor accepting a superclass instance (InputStream object) as a parameter.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @Andrea - There's no need for `FileReader` and `FileInputStream` to be related; a constructor can do anything it wants, including creating instances of completely-unrelated classes. Here, `FileReader` is creating a `FileInputStream` (not in its hierarchy) so it can pass it as an *argument* to `super(InputStream)` (which is `InputStreamReader(InputStream)`). I suggest reading through [this tutorial](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) on the Java site and working through some others and/or a good beginner's Java book. Happy coding! – T.J. Crowder May 13 '18 at 13:19
  • @Andrea - Not at all! We're all new once! :-) – T.J. Crowder May 13 '18 at 13:37
  • @Crowder, Last question....When I call FileReader Constructor like this FileReader(String fileName), Then different instances are getting created, 1) FIleReader itself 2) InputStreamReader 3) FileInputStream. So May I know please how they gets lined up for reading a content from file ? I Mean which comes firstly connected to source file and so on....! – Andrea May 13 '18 at 13:55
  • @Andrea - The `FileReader` **is** the `InputStreamReader`, they're the same object. That object creates a `FileInputStream` to do the actual work of reading the file data; it does some additional work so it can provide the `Reader` (character-oriented) API rather than the `InputStream` (byte-orientated) API. – T.J. Crowder May 13 '18 at 14:01
  • @Crowder, So I can say...FileInputStream is wrapped by FIleReader. Correct..? The abstract model would be like this if am not wrong...Filereader----->FileInputStream---->SourceFile. But if that's a case then how both coordinates with each other because one reads bytes and other Char....? Secondly am not able to understand your this comment "it does some additional work so it can provide the Reader (character-oriented) API rather than the InputStream (byte-orientated) API"...Can you please elaborate little more ? – Andrea May 13 '18 at 14:12
  • @Andrea - Sorry, I think you just need to spend some time with tutorials and such. By yes, conceptually, `FileReader` -> `FileInputStream` -> the file. – T.J. Crowder May 13 '18 at 14:23