• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.in.read() - Abstract method?

 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are these programs the same?
Why is it possible to say InputStream in = System.in. Is System.in an object which can be referenced?

P.S: I found a good posting on read() called "Abstract method read()" but I don't understand it. So i post this question.
Thanks.
Thomas
[ Edited to preserve formatting using the [code] and [/code] UBB tags. It'd be nice if Thomas would start doing this all by himself -ds ]
[ July 30, 2002: Message edited by: Dirk Schreckmann ]
 
Neil Laurance
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class java.lang.System class contains the following 3 public constants:
public static final java.io.PrintStream err;
public static final java.io.InputStream in;
public static final java.io.PrintStream out;
What this means is that System.in is a type of InputStream. In fact, it will be a concrete subclass of this abstract class. However, you can just use the methods of InputStream, and the underlying methods will be called due to polymorphism. Similarly, you can create an alias to the System.in InputStream handle with the code:
InputStream in = System.in;
What this means is that in and System.in are both handles to the same underlying object.
Hope this hasn't confused you more...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic