Results 1 to 6 of 6
  1. #1
    oneandonly is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by oneandonly View Post
    read() is an abstract method of InputStream class.
    [ ... ]
    Confusion is that if read method of InputStream is abstract how can we call the read method of inputstream class
    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

  3. #3
    oneandonly is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    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 ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by oneandonly View Post
    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 ?
    You could've written this instead:

    Java Code:
    InputStream x=new FileInputStream();
    x.read();
    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.

    kind regards,

    Jos

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    But which subclass read method (out of numerous ones) is getting called when using the i.read()method ?
    Jos gives you (and he always does) very good answer. :cool:

    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 04:04 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by oneandonly View Post
    But which subclass read method (out of numerous ones) is getting called when using the i.read()method ?
    Doesn't matter.
    You don't need to know.
    That's the point of coding to interfaces (well, abstract class in this case)...you only need to know that it fulfills the contract given by the API of InputStream.

Similar Threads

  1. static method within same package not read
    By jon80 in forum New To Java
    Replies: 11
    Last Post: 06-15-2009, 10:47 AM
  2. TCP/IP Client program halts at the read method of the stream
    By PradeepBadiger in forum Networking
    Replies: 0
    Last Post: 03-26-2009, 04:14 PM
  3. Replies: 3
    Last Post: 01-29-2009, 11:20 AM
  4. Replies: 5
    Last Post: 10-17-2008, 03:13 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •