1

This is the code I found about reading bytes from Stream.

import java.io.*;

InputStream in = new FileInputStream("f://myfile.txt");
int b;

while ((b = in.read()) != -1) {
    System.out.println("The next byte is " + b);
}

in.close();

Although, we know that we're dealing specifically with a FileInputStream,why we are referring an instance of FileInputStream as an instance of InputStream?

InputStream in = new FileInputStream("f://myfile.txt");
//rest of the code   

Instead of simply doing this:

FileInputStream in = new FileInputStream("f://myfile.txt");
//rest of the code

if I am correct, then this is called Upcasting because we are casting an object of subclass type to its super class? But why are we doing this??

Does it enhance the performance?

Same thing is done in this Answer(Download Files from internet) while downloading files form internet in Android.

Community
  • 1
  • 1
Naresh
  • 941
  • 9
  • 22
  • `If I am correct, then this is called Upcasting`. Well i dont know what it is called but rather then upcasting i would consider that downcasting. – greenapps Jan 17 '17 at 09:09
  • @greenapps I am sure it's called upcasting.you can refer this document [link](http://stackoverflow.com/a/40394155/7379309) – Naresh Jan 17 '17 at 09:16

0 Answers0