[.net] new Method() vs. virtual / override

Started by
2 comments, last by capn_midnight 18 years, 8 months ago
If I'm not mistaken, in C# you can override a method of the subclass by typing: new MethodName() { // blah } inside the inheriting class. what are the differences between this and making the original member function virtual and use the override keyword when typing the new version? thanks, unshaven
Advertisement
Using "new" does not support polymorphism.

using System;namespace TestSpace{	class A	{		public virtual void print1()		{			Console.WriteLine("A1");		}		public void print2()		{			Console.WriteLine("A2");		}		public void print3()		{			Console.WriteLine("A3");		}	}	class B : A	{		public override void print1()		{			Console.WriteLine("B1");		}		public new void print2()		{			Console.WriteLine("B2");		}	}	/// <summary>	/// Summary description for Class1.	/// </summary>	class Class1	{		/// <summary>		/// The main entry point for the application.		/// </summary>		[STAThread]		static void Main(string[] args)		{			B b = new B();			A a = new A();			A ab = b;			a.print1();			a.print2();			a.print3();			b.print1();			b.print2();			b.print3();			ab.print1();			ab.print2();			ab.print3();		}			}}

output:A1A2A3B1B2A3B1A2A3

notice that ab.print1() calls B's print1() and ab.print2() calls A's print2().

[Edited by - capn_midnight on August 16, 2005 8:23:05 AM]

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

By using virutal and override you adhear to oo principals. The new keyword is only meant for versioning issues where you derive from a class and there are methods that are using the same method names as in your sub class. By using the new keyword you are telling the compiler to ignore the base class method and use yours instead.

You should only use new if you do not have control of the base class or you want to make sure that you dont break any code of the users your base class.
Quote:Original post by MreZ
By using virutal and override you adhear to oo principals. The new keyword is only meant for versioning issues where you derive from a class and there are methods that are using the same method names as in your sub class. By using the new keyword you are telling the compiler to ignore the base class method and use yours instead.

You should only use new if you do not have control of the base class or you want to make sure that you dont break any code of the users your base class.

It's a feature called dynamic dispatch, and like C++ (and unlike SmallTalk), it's off by default. Specifically, it incurs a lot of overhead, because the specific method invokation is determined at runtime, not compile time (as demonstrated in my code example above). So, I wouldn't say lack of integration control over a base class is the only reason one would want to avoid using pure virtual functions.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement