Results 1 to 12 of 12
  1. #1
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default Defining the inputStream object

    Question regarding extend issue

    What is the reason behind defining the inputStream object in this way...

    Java Code:
    InputStream inputStream = new FileInputStream("c:\\input.txt");
    I know that FileInputStream extends InputStream, is there anything else ?

    I mean we could have define it like

    Java Code:
    FileInputStream inputStream = new FileInputStream("c:\\input.txt");
    Thank you

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

    Default

    If you don't need functionality from the FileInputStream, that is there's no specific methods required, then simply using InputStream is viewed as best practice, since you know that changing the right hand side to new SomeOtherInputStream() won't affect the rest of the code.

    For the same reason, methods should generally return an interface (ie List rather than ArrayList) so it allows you to change the specific class if you feel ArrayList isn't performing well enough...

    That's probably a rubbish explanation, so I apologise in advance!
    :)

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

    Default

    Quote Originally Posted by Tolls View Post
    If you don't need functionality from the FileInputStream, that is there's no specific methods required, then simply using InputStream is viewed as best practice, since you know that changing the right hand side to new SomeOtherInputStream() won't affect the rest of the code.

    For the same reason, methods should generally return an interface (ie List rather than ArrayList) so it allows you to change the specific class if you feel ArrayList isn't performing well enough...

    That's probably a rubbish explanation, so I apologise in advance!
    :)
    It's not rubbish at all; there's the old mantra "code to the interface, not to the implementation" and the same holds for classes: always treat an object as the most abstract class (i.e. a class more to the root (Object)) that still fullfills your needs. A FileInputStream is an InputStream and most of the time you want to treat it that way; you can't treat is an Object because you can't read from an Object, nor can you close it etc. The entire I/O framework was designed as a bunch of wrappers and you can treat the classes in the framework as such.

    kind regards,

    Jos

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    It's an example of coding to the interface rather than to an implementation which enhances code reuse.
    In this example you can reuse the inputStream variable in later parts of the code for other InputStreams that are not FileInputStreams.
    A more useful example is in parameter types for methods.
    If a method needs, say a List, and is able to perform its operations by using only the methods declared in the List interface, then it is better to declare it's argument as List rather than, say, ArrayList because that allows you (and other people) to use it with any implementations of List without rewriting specific methods for each List implementation.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Give me back my faster typing fingers Jos.

    I promise that I won't blow the vuvuzela again. I'll even find time to review the code for ... <fingers too slow to finish this sentence in time>

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

    Default

    Quote Originally Posted by JosAH View Post
    It's not rubbish at all;
    I know the concept wasn't rubbsih, just that my stream-of-consciousness, aka mashing-the-keyboard, explanation might have been.
    :)

  7. #7
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default

    Thank you for Tolls JosAH for the helpful replys...
    Really helped ...

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

    Default

    Quote Originally Posted by Tolls View Post
    I know the concept wasn't rubbsih, just that my stream-of-consciousness, aka mashing-the-keyboard, explanation might have been.
    :)
    Ah, yes, granted, the style of you reply/explanation was a bit bourgeois; my writing style is much more eloquent of course ;-)

    kindest regards,

    Jos

  9. #9
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default

    Dear r035198x

    In this example you can reuse the inputStream variable in later parts of the code for other InputStreams that are not FileInputStreams
    but I can still reuse the variables of both FileInputStreams and InputStreams if I created the object in usual method, right ?

    Thank you

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

    Default

    Quote Originally Posted by JosAH View Post
    Ah, yes, granted, the style of you reply/explanation was a bit bourgeois; my writing style is much more eloquent of course ;-)

    kindest regards,

    Jos
    Let me see, how can I put this?

    ...**thrrrrrrrrrrrp**...
    :p

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

    Default

    Quote Originally Posted by r035198x View Post
    Give me back my faster typing fingers Jos.

    I promise that I won't blow the vuvuzela again. I'll even find time to review the code for ... <fingers too slow to finish this sentence in time>
    The first person who is going to honk on a vuvuzela in my vicinity again is going to get the thing stuffed deep in his/her throat.

    b.t.w. I'm going to upload yet another version of the stuff; nothing special, just bug fixes and a few enhancements in the expression manipulation stuff.

    kind regards,

    Jos

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by yahyaaa View Post
    Dear r035198x


    but I can still reuse the variables of both FileInputStreams and InputStreams if I created the object in usual method, right ?

    Thank you
    If you declare it as a FileInputStream then you can only reuse it to create other FileInputStreams. If you declare it as InputStream, then you can reuse it for FileInputStreams and other InputStreams that are not FileInputStream

Similar Threads

  1. Creating an InputStream
    By c26354 in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 05:48 AM
  2. Java InputStream
    By Bill88 in forum New To Java
    Replies: 10
    Last Post: 09-21-2009, 03:40 PM
  3. defining own exception
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 01:47 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
  •