Re: Virtual function call from constructor
Mike Schilling wrote:
"Twisted" <twisted0n3@gmail.com> wrote in message
news:1182724872.690108.309670@k79g2000hse.googlegroups.com...
Think of it this way. When the Object constructor is running, the
instance
*is* an Object. It may have some extra space allocated at the end, but
no
one can make any use of it. Now, when the OutputStream constructor is
running, the instance *is* an OutputStream. It may have some extra space
allocated at the end, but no one can make any use of it. etc. Finally,
when
the PrintStream constructor runs, it *is* a PrintStream.
Except that in Java it's a PrintStream from the outset, and is not a
vanilla Object even when Object's constructor is not finished yet.
(Does Object even have a nontrivial constructor?)
That's presumably a JVM-specific question.
No; that's the Java language. If Object's constructor
ultimately chained from a PrintStream constructor were to
evaluate `this instanceof PrintStream' the result would be
`true', on every JVM. (Object's constructor has no reason
to do any such thing, but that's another matter.)
You can test the pattern with
class Super {
Super() {
System.out.println((this instanceof Super)
+ ", " + (this instanceof Sub));
}
public static void main(String[] unused) {
new Super();
new Sub();
}
}
class Sub extends Super {
}
--
Eric Sosman
esosman@acm-dot-org.invalid