• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding

 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.OutputStream;
import java.io.IOException;
class BufferOutput {
private OutputStream o;
BufferOutput(OutputStream o) { this.o = o; }
protected byte[] buf = new byte[512];
protected int pos = 0;
public void putchar(char c) throws IOException {
if (pos == buf.length)
flush();
buf[pos++] = (byte)c;
}
public void putstr(String s) throws IOException { //line3
for (int i = 0; i < s.length(); i++)
putchar(s.charAt(i));
}
public void flush() throws IOException {
o.write(buf, 0, pos);
pos = 0;
}
}
class LineBufferOutput extends BufferOutput {
LineBufferOutput(OutputStream o) { super(o); }
public void putchar(char c) throws IOException {
super.putchar(c); //line0
if (c == '\n')
flush();
}
}
class Test {
public static void main(String[] args)
throws IOException
{
LineBufferOutput lbo =
new LineBufferOutput(System.out);
lbo.putstr("lbo\nlbo"); //line1
System.out.print("print\n");
lbo.putstr("\n");
}
}
why is it printing lbo,print,lbo?
How many times line1 will get called?
I am assuming line1 will call line0 which in turn call line3? I am right, Please explain me.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your post to put UBB 'code" tags around your code. As it is now, it's very hard to read.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suresh:
Please note following:
1. putstr is inherited. But putchar is overridden. Therefore, putstr is called from base class. Within putstr, when a call is made for putchar, the one in derived classed will be invoked.
2. putchar in derived class has modified behavior. That is when it see \n, it prints the ouput. That is how you get your "lbo" printed and print cursor goes to next line.
3. After that next "lbo" is stored in buffer but not printed because there is no \n at the end of string.
4. Next line in main() prints "print" and cursor goes to next line.
5. Next line in main() causes the derived putchar to print what is in the buffer at that time which second "lbo". Therefore, you get last line printed that is "lbo".
Hope this helps.
Barkat
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to Barkat explanation from point 1 "when a call is made for putchar, the one in derived classed will be invoked."
but the call has
super.putchar(c); statement. I was assuming that it is going to call its superclass putchar method. Is that not true?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. And then control will come back in derived class and check for \n. That is the modified part.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic