class Base {
public foo() {}
}
class Derived : Base {
public foo() {}
// vs.
public new foo() {}
}
As I understand it, based on the runtime type of the
object the most derived version of foo() would be called
anyway. There seems to be no way of enforcing the
prevention of calling the base classes method as:
((Base)obj).foo() works.
Thanks in advance.
Hi,
Hiding a method allocates a new "slot" in the method table, so that calls
that are bound to the original method will never get handled by the new
method. For example (pseudo-script):
class Base
function foo { print "Base.foo" }
function bar { print "Base.bar" }
class Derived extends Base
function foo { print "Derived.foo" }
new function bar { print "Derived.bar" }
b = new Base
d = new Derived
b.foo // Base.foo
b.bar // Base.bar
d.foo // Derived.foo
d.bar // Derived.bar
b = d
b.foo // Derived.foo, because it overrides
b.bar // Base.bar, because it Derived hides it
> There seems to be no way of enforcing the
> prevention of calling the base classes method as:
> ((Base)obj).foo() works.
This will work for a hidden method, but for an overridden method you will
always call the most-derived implementation. This is a security issue.
Peter
--
Peter Torr -- pt...@microsoft.com -- JScript .NET / VSA Runtime
Suffering from insomnia? http://www.microsoft.com/info/cpyright.htm
Please post all questions to the group. Thanks.