This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
It is polymorphism
Posted by Kishori Sharan on January 10, 2001 at 7:10 PM
When you write
then it means that you have declared a variable in which you can hold a reference to an object of class InputStream or any of its sub class or a null reference. You are right you cannot instantiate InputStream class becuase it is abstract. But "in" can refer to an object of InputStream class or its sub class. In this case it will always refer to an object of sub class of InputStream. Since "in" is an object reference of type InputStream that is why you can call in.read ( ) method because read method is defined in InputStream class.
in = url.openStream()
At run time url.openStream ( ) will return an object reference which must be a sub class of InputStream. To know its class just write the following code
System.out.println ( in.getClass().getName() ) ;
This will print the actual class name of which it holds the reference at run time.