aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
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

Joined: Jun 06, 2002
Posts: 1056
Please edit your post to put UBB 'code" tags around your code. As it is now, it's very hard to read.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jul 30, 2001
Posts: 149
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

Joined: Aug 05, 2002
Posts: 787
Yes. And then control will come back in derived class and check for \n. That is the modified part.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding