[Mono-list] Re: Final method in C# (as the final keyword in Java)

Robert Jordan robertj at gmx.net
Tue May 30 10:24:01 EDT 2006

Ympostor wrote:
> Raja R Harinath escribió:
>>> Son oSon = new Son();
>>> Son.DontOverrideMe();
> Typo here, the correct would be:
> oSon.DontOverrideMe();
>>> The result would be "unruly son".
>> OK.  That's what the 'new' keyword does.
>>> I don't want that, I want that all derived classes of Father return "I
>>> am acting as a father" to the console, and I want the compiler to
>>> complain if a derived class wants to hide or override the method.
>> There are two ways to respond.
>>   1. You can't -- since C# has a special modifier 'new' that allows
>>      child classes to "escape" the tyranny of parent classes :-)
>>   2. Why do you care?  If you're using DontOverrideMe polymorphically
>>      through a 'GrandFather' or 'Father' reference, you'll get the right
>>      behaviour even with a 'Son' object.
> Thanks Hari, that clarifies all a bit more!
> Anyway, I have more questions if you don't mind :)
> 1) How can I call DontOverrideMe by a the Father reference?

Father f = new Son ();
f.DonOverrideMe ();

> 3) How can I avoid declaring a GrandFather class (now I am using it 
> because otherwise I am unable to use the sealed keyword in a method).

Make the method non-virtual.


More information about the Mono-list mailing list