This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about InputStream

 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic