Results 1 to 6 of 6
Thread: InpuStream read method
- 08-01-2010, 01:41 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
InpuStream read method
read() is an abstract method of InputStream class.
How come this code can work ?
Runtime r= Runtime.getRuntime();
Process p= r.exec("hostname");
InputStream i = p.getInputStream();
while(i.read()) // <--- how is this possible
{
//do something
}
Confusion is that if read method of InputStream is abstract how can we call the read method of inputstream class
- 08-01-2010, 01:51 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,412
- Blog Entries
- 7
- Rep Power
- 17
The trick is that you don't get an InputStream class but a concrete sub class thereof (it has the read() method implemented) but for all you care it is an InputStream. This is part of the 'object oriented' programming paradigm: the sub class is a 'specialization' of the super class but conceptually it also 'is' the superclass. e.g. A banana is a (piece of) fruit. You can consider a banana a sort of fruit.
kind regards,
Jos
- 08-01-2010, 02:14 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
Thanks for quick reply but don't we expect this behavior if we use some subclass of InputStream class(e.g FileInputStream).
I mean to say if i use
FileInputStream x=new FileInputStream();
x.read();
then its acceptable that since FileInputStream is a child class and implementing the InputStream read method.
But which subclass read method (out of numerous ones) is getting called when using the i.read()method ?
- 08-01-2010, 02:25 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,412
- Blog Entries
- 7
- Rep Power
- 17
You could've written this instead:
because you're only interested in that object being an InputStream; it has a read() method and you don't care whether it's abstract or not. You know that your FileInputStream implements it. If you really care about what class exactly is passed back to you you can call its getClass() method.Java Code:InputStream x=new FileInputStream(); x.read();
kind regards,
Jos
- 08-01-2010, 03:00 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Jos gives you (and he always does) very good answer. :cool:Java Code:But which subclass read method (out of numerous ones) is getting called when using the i.read()method ?
http://download.oracle.com/javase/1....am.html#read()
On this link you can see that you have three read() functions with different arguments. First one is abstract, but second and third are not abstract.Last edited by cselic; 08-01-2010 at 03:04 PM.
- 08-02-2010, 11:37 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
static method within same package not read
By jon80 in forum New To JavaReplies: 11Last Post: 06-15-2009, 09:47 AM -
TCP/IP Client program halts at the read method of the stream
By PradeepBadiger in forum NetworkingReplies: 0Last Post: 03-26-2009, 03:14 PM -
How can I call my database read method to display its ArrayList?
By matpj in forum New To JavaReplies: 3Last Post: 01-29-2009, 10:20 AM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks