Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Method Hiding/Versioning

1 view
Skip to first unread message

Rix

unread,
Aug 5, 2001, 12:53:58 PM8/5/01
to
How is simply overriding a base classes method any
different from hiding it via versioning.

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.

Peter Torr (MS)

unread,
Aug 6, 2001, 8:37:15 PM8/6/01
to
"Rix" <rbra...@btbconsulting.com> wrote in message
news:dbd501c11dcf$384423b0$19ef2ecf@tkmsftngxa01...

> How is simply overriding a base classes method any
> different from hiding it via versioning.

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.

0 new messages