I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 219, comments - 2287, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

How to Override Non-Virtual Base Members in C#

This is something i assumed was common knowledge, but apparently it isn't.

If you want to have a method in your derived class with the same name as the one in the base class and the base class one isn't marked

as virtual you can use the NEW keyword.

This is also the default behaviour and the compiler treats the child class method as new, however you get a warning for this.

 

Some code to illustrate this:

 

public class BaseClass
{
    public BaseClass()
    { }

    public void IAmANonVirtualMethod()
    {
        Console.WriteLine("IAmANonVirtualMethod in BaseClass.");
    }

    public virtual void IAmAVirtualMethod()
    {
        Console.WriteLine("IAmAVirtualMethod in BaseClass.");
    }
}
    
public class ChildClass : BaseClass
{
    public ChildClass()
    { }

    public new void IAmANonVirtualMethod()
    {
        Console.WriteLine("IAmANonVirtualMethod in ChildClass.");
    }
}

BaseClass bc = new BaseClass();
ChildClass cc = new ChildClass();
bc.IAmANonVirtualMethod();
cc.IAmANonVirtualMethod();
bc.IAmAVirtualMethod();
cc.IAmAVirtualMethod();


/* Output 

IAmANonVirtualMethod in BaseClass.
IAmANonVirtualMethod in ChildClass.
IAmAVirtualMethod in BaseClass.
IAmAVirtualMethod in BaseClass.
*/

Print | posted on Monday, April 09, 2007 7:08 PM | Filed Under [ .Net ]

Feedback

Gravatar

# re: How to Override Non-Virtual Base Members in C#

Carefull with this though it might not work as advertised here, when the base class calls the function overriden with the new keyword its selve the implementation in the baseclass is called not the implementation in the childclass.

When you add

public void CallMethods()
{
IAmANonVirtualMethod();
IAmAVirtualMethod();
}

That function to the baseclass and call it on both in the main function you'll see that the implementation overriden in the child class is never called.

4/9/2007 11:36 PM | Ray
Gravatar

# re: How to Override Non-Virtual Base Members in C#

Using the "new" keyword is not same as overriding... "new" just *adds* additional method (with the same name) to the derived class, whereas overriding a virtual method actually overrides it. Using "new" keyword may sometimes lead to confusing situations - try the following code lines with your classes:

ChildClass cc = new ChildClass();
BaseClass bcc = new ChildClass();
cc.IAmANonVirtualMethod();
bcc.IAmANonVirtualMethod();
4/10/2007 12:59 AM | Andrej Tozon
Gravatar

# re: How to Override Non-Virtual Base Members in C#

@Ray:
Of course it's not.
As Andrej pointed out it's a new method in the base class.

@Andrej:
have you ever had to acctually use this form:
BaseClass bcc = new ChildClass();
I really hate writing code like that. :))

4/10/2007 10:03 AM | Mladen
Gravatar

# re: How to Override Non-Virtual Base Members in C#

Mladen - yes, I do it all the time... not necessariliy in the form I previously posted, but consider this (somehow silly) example for illustration:

Classes:

public class Dog
{
public void Bark()
{
Console.WriteLine("Growl!");
}
}

public class Puppy : Dog
{
public new void Bark()
{
Console.Write("Woof!");
}
}


Calling class:

static class Program
{
[STAThread]
static void Main()
{
Dog dog = new Dog();
MakeHimBark(dog);

Puppy puppy = new Puppy();
MakeHimBark(puppy);
}

public static void MakeHimBark(Dog theDog)
{
theDog.Bark();
}
}

The output, of course is - 2x Growl, even when I instantiated Puppy as a puppy. However, overriding Bark method in Puppy class (if it was marked virtual), would give expected result (Growl, Woof).
4/10/2007 12:20 PM | Andrej Tozon
Gravatar

# re: How to Override Non-Virtual Base Members in C#

"have you ever had to acctually use this form:
BaseClass bcc = new ChildClass();
I really hate writing code like that. :))"

that's what polymorphism is all about. if you hate it, you hate a pretty major aspect of OOP. :)

4/10/2007 6:08 PM | Jesse
Gravatar

# re: How to Override Non-Virtual Base Members in C#

ok so why couldn't you just write:
ChildClass bcc = new ChildClass();
instead of:
BaseClass bcc = new ChildClass();

ok i guess there are moments where this is usefull, but i see no advantage
of 2nd vs the 1st.
4/11/2007 9:27 AM | Mladen
Gravatar

# re: How to Override Non-Virtual Base Members in C#

The difference is that later version can host an instance of any BaseClass derived class while the former can host only ChildClass (derived) class.
Imagine that your instance type is unknown at design time (perhaps based on user choice). All you know is that it will be a instance of BaseClass derived class. You'll want to declare such variable as of BaseClass type.
However, if you are certain that you'll have a ChildClass instance only (or its derived class instance) then your are better with the former.
4/11/2007 5:04 PM | Miha Markic
Gravatar

# re: How to Override Non-Virtual Base Members in C#

"... but i see no advantage..."

This is not about advantage, it's about possibilities. In my Dog/Puppy example above, the MakeHimBark method will accept any Dog class (base Dog or *any* derived class), knowing that each and every one will have the Bark method implemented. The same goes when you're instancing objects at run-time and/or casting them into BaseClass (Dog). Good example of this is some kind of a plug-in framework, where all plug-ins derive from the same type.
The bottom line is, that using "new" may sometimes lead to unpredictible results for developer if he's not familiar with how such implementation behaves at run-time...
4/12/2007 1:16 AM | Andrej Tozon
Gravatar

# re: How to Override Non-Virtual Base Members in C#

ok i must cover myself with dust since i've used exactly this scenario in my latest pet project :))

Your bottom line is correct and agreeable Mr. MVP :))
4/12/2007 9:51 AM | Mladen
Gravatar

# re: How to Override Non-Virtual Base Members in C#

What I'm getting out of this thread is that the title of this post is incorrect; there is no way to "override" a non-virtual base class method. Said another way, there's no way to get polymorphic behavior on a method if the base class method was not declared as virtual. Is this correct?
12/20/2007 4:33 PM | Tobey
Gravatar

# re: How to Override Non-Virtual Base Members in C#

yes.
12/20/2007 4:36 PM | Mladen
Gravatar

# re: How to Override Non-Virtual Base Members in C#

You can "override" non virtual members IF those are part of an interface, by Inheriting AND implementing the interface using the new keyword. But you are really "overriding" the interface method.

public class UniqueList<T> : List<T>, IList<T>
{
public new void Add( T item )
{
if( base.Contains(item) )
{
throw new ArgumentException("non unique");
}
base.Add( item );
}
}

/Jonas
3/25/2009 8:20 PM | Jonas
Gravatar

# re: How to Override Non-Virtual Base Members in C#

Followed code segment has the same behavior with or without IDictionary Interface

namespace VirtualTst
{
public enum IndexTypes{
ruge = 0,
coarse = 1,
comp = 3
}
public class Derived : Dictionary&amp;lt;IndexTypes,int&amp;gt;,IDictionary&amp;lt;IndexTypes,int&amp;gt;
{
public new void Add(IndexTypes IndexType, int Value)
{
Console.WriteLine(&amp;quot;Derived&amp;quot;);
base.Add(IndexType,Value);
}
}
class Class1
{
public static void Main(string[] args)
{
Derived d = new Derived();
d.Add(IndexTypes.coarse, 3);
Dictionary&amp;lt;IndexTypes,int&amp;gt;b = d;
Console.WriteLine(&amp;quot;Try cast to Dictionary&amp;quot;);
b.Add(IndexTypes.ruge, 3);
}
}
}

Any suggestions to fix it?
7/13/2009 3:50 PM | Roman
Gravatar

# re: How to Override Non-Virtual Base Members in C#

public class clsAddEditSalary : clsMainAbstract
{
public clsAddEditSalary()
{
//
// TODO: Add constructor logic here
//
}
public override int Display(int j)
{
return j;
}
public override int Display1(int j)
{
return j;
}
public new int Display2(int j)
{
return j;
}
}


public abstract class clsMainAbstract
{
public clsMainAbstract()
{
//
// TODO: Add constructor logic here
//
}
public abstract int Display(int j);
public virtual int Display1(int j)
{
return j + 2;
}
public int Display2(int j)
{
return j + 2;
}
}

clsMainAbstract objAbs = new clsAddEditSalary();
objAbs.Display(9);>>9
objAbs.Display1(10);>>10
objAbs.Display2(11);>>13
clsAddEditSalary objInh = new clsAddEditSalary();
objInh.Display(12);>>12
objInh.Display1(13);>>13
objInh.Display2(14);>>14

Can Some body explain how the output is coming?

Regards,
Lucy
9/10/2009 9:04 AM | Lucy
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET