Jump to content
  • Advertisement
Sign in to follow this  
UnshavenBastard

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

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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:
A1
A2
A3

B1
B2
A3

B1
A2
A3

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!