aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes read() method   question (reposting) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "read() method   question (reposting)" Watch "read() method   question (reposting)" New topic
Author

read() method question (reposting)

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
when one calls the read() method, typically say in the System.in.read() then which read method is being called and from where ? because the read() method is abstract (as mentioned in the api).
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by mark stone:
when one calls the read() method, typically say in the System.in.read() then which read method is being called and from where ? because the read() method is abstract (as mentioned in the api).

You call its implementation in one of the subclasses. In class System, "in" happens to be a member static class of type InputStream, as it is the topmost superclass it can have reference to any of its subclasses.
HTH,
- Manish
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
say i have a class Test in that i have something like this below. the code below is fine. My question is that i have not specified any subclass of InputStream.
actually a subclass can call a method in its superclass (via inheritance) and not the other way.
ok just let me know in the code below which class's read() is being called. because the api says that when read method is called on an object of class InputStream. And System.in give us an object of InputStream. right ?
import java.io.*;
public class Test {
public static void main(String args[])
{try{
int i =0;
System.out.println("Enter here");
while((i=System.in.read()) != -1)
{System.out.println(i);}}
catch(IOException e)
{System.out.println("Err-1");}
---------------------------------------------

Originally posted by Manish Hatwalne:

You call its implementation in one of the subclasses. In class System, "in" happens to be a member static class of type InputStream, as it is the topmost superclass it can have reference to any of its subclasses.
HTH,
- Manish
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Does this help.....

The output will be:
Class of in Object is : class java.io.BufferedInputStream
So the System.in object is of class BufferedInputStream on my system JDK which implements the read() method.
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Forgot to add this is the RunTime behavior.
Since BufferedInputStream is a subclass of InputStream, it is valid.
regds.
- satya
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
thanks. let me sit back and analyse this at length. it was bothering me for a while but hopefully i think this should be it. if not shall write back. thanks again.

Originally posted by Madhav Lakkapragada:
Forgot to add this is the RunTime behavior.
Since BufferedInputStream is a subclass of InputStream, it is valid.
regds.
- satya
Zheng Xu
Greenhorn

Joined: Feb 05, 2002
Posts: 4
Look at the System.java source file, you will find
/**
* Initialize the system class. Called after thread initialization.
*/
private static void initializeSystemClass() {
...
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
setIn0(new BufferedInputStream(fdIn));
...
}
and setIn0 is a native method, which I think is to set System.in
So finally System.in.read() is not a abstract method, through overriding it calls the native read() method in FileInputStream and read from the terminal which is set through FileDescriptor.in
Zheng Xu
 
wood burning stoves
 
subject: read() method question (reposting)