|
Re: java virtual methods
|
Posted: Jun 11, 2005 6:50 AM
|
|
> > In Java, virtual methods do just work in their normal > way > > in constructors (as Mr Meyers implied). But any member > > variables used wouldn't be fully initialized. > > Which is the problem - in C++, C# and Java, then. This is > the heart of why I think C++ is right, C# and Java wrong > in this regard.
In Java, there is the notation, "<ClassName>.this." that can be used to qualify an inner class reference to an outer class instance.
Unfortunately, this notation is not defined for class hierarchy references related to inheritance. If it was, then you'd be able to code
class Derived extends Base {
public Derived() {
buf = new StringBuffer();
Derived.this.Log();
}
public void Log() {
buf.append( "Derived");
System.out.println(buf);
}
}
and get the desired behavior. The flexibility of explicitly being able to designate the desired behavior would be a plus for me.
However, once you know the rules, Java's and C#'s behaviors are not nearly destructive as C++ would be if it acted like Java and C#. C++'s behavior is an acknowledgement of the executing environment and the hazards of a NULL reference where there is no language protection from the side effects of such, as Java and C# provide.
|
|