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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about InputStream" Watch "question about InputStream" New topic
Author

question about InputStream

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
the class InputStream is abstract.
but then why have they defined a constructor for it in the api. why ? Because one cannot instantiate this class, so what's the point for this constructor ?
Rex Rock
Ranch Hand

Joined: Jun 25, 2001
Posts: 82
Let me give this a shot. i may be wrong, but I think I'm right
well, you could define a class that extends from InputStream like this

now within the constructor of x, the default constructor of InputStream will be called,even if you dont call it explicitly, and am assuming there are some settings and initializations that needs to be done for file i/o, which is done in the InputStream's constructor.
hope this helps
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
Originally posted by mark stone:
the class InputStream is abstract.
when one calls the read() method, typically say in the System.in.read() then which read method is being called ? because the read() method is abstract (as mentioned in the api).
the other read methods in InputStream class
read(byte[] b) and read(byte[] b, int off, int len) are not abstract though. I know that they are not being called, ijust mentioned it because my next question deals with this.
second question about the buffer b. We could access the array b ? right ? is this why this buffer has been provided so that one may access the contents of this buffer ? if not what is the purpose.


Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by mark stone:
the class InputStream is abstract.
but then why have they defined a constructor for it in the api. why ? Because one cannot instantiate this class, so what's the point for this constructor ?

<CODE>
class X extends InputStream {
public X(){
super();
}
public int read() {
return 1;}
public static void main(String args[]) {
InputStream is = new X();
//InputStream ist = new InputStream();
}
}
</CODE>
Abstract class cant be instantiated
But their constructors are used when the sub-classes are instantiated
Ragu
PS: Valentin made a wonderful expln about this issue a while back, please use the search engine for further expln:
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
got it ragu.
but the the call to super() could be avoided because when constructor X() is called the call to super() will be made (by the compiler)
right ?
class X extends InputStream {
public X(){
super();
}
Originally posted by Ragu Sivaraman:

<CODE>
class X extends InputStream {
public X(){
super();
}
public int read() {
return 1;}
public static void main(String args[]) {
InputStream is = new X();
//InputStream ist = new InputStream();
}
}
</CODE>
Abstract class cant be instantiated
But their constructors are used when the sub-classes are instantiated
Ragu
PS: Valentin made a wonderful expln about this issue a while back, please use the search engine for further expln:
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Oh absolutely..
super() is redundant here. If you had a specalty constructor, then using super with the specalty signature will alone let you compile.. That's why i wrote super()...
Ragu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about InputStream