OK, so I'm somewhat experienced in Java, and am learning C#. I'm currently on the topic of Inheritance and Polymorphism. What I read is that if a base class has a method that is marked as virtual, then the inheriting class has the choice to override that method if it so desires. What I also read is that if you want to override said method, you need to add the override keyword to the method line, or new if you want to shadow the method. What I don't understand, is that during tests, the program will function the same exact way whether I add override new or I don't add it at all. I can still call base.methodname(); without complaint. All I get is a warning from Visual Studio that the child method is shadowing the parent one. No error.

What's the point, then, in having these keywords?

Re: virtual/new/override 80 80

You know i've been wondering that for a while I just never looked it up, but here it is:

It is an error to use both new and override on the same member because the two modifiers have mutually exclusive meanings. The new modifier creates a new member with the same name and causes the original member to become hidden. The override modifier extends the implementation for an inherited member.

I think that means they are basically the same -- but it provides a difference in documentation for a member if it was intended to be extended or if it was hidden

Re: virtual/new/override 80 80

No wait -- there is a difference.

Take this example class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace daniweb
{
  public class skBaseClass
  {
    public skBaseClass()
    {
    }
    public virtual void Test1()
    {
      Console.WriteLine("skBaseClass.Test1()");
    }
    public void Test2()
    {
      Console.WriteLine("skBaseClass.Test2()");
    }
  }
  public class skInherit : skBaseClass
  {
    public skInherit()
      : base()
    {
    }
    public override void Test1()
    {
      Console.WriteLine("skInherit.Test1()");
    }
    public new void Test2()
    {
      Console.WriteLine("skInherit.Test2()");
    }
  }
}

Running this:

private void button4_Click(object sender, EventArgs e)
    {
      skInherit sk = new skInherit();
      skBaseClass s = (sk as skBaseClass);
      s.Test1();
      s.Test2();
    }

outputs this:

skInherit.Test1()
skBaseClass.Test2()

The overriden member calls the inherited classes method, where as when i cast the inherited class to the base class and call the method... it calls the base method. They are different.

Re: virtual/new/override 80 80

OK, so the new keyword clearly has a meaning, because calling the base class's method runs the child one anyhow. But unless i'm mistaken override doesn't really seem to be necessary. Couldn't you just declare a method in the child class with the same signature as the parent one, and it would run that one instead if calling child.method(); ?

My point is that Visual Studio only gave me a warning stating that the child had a method with the same name as the method in the parent, and it was telling me to either override the parent or create a new instance. It worked regardless.

Re: virtual/new/override 80 80

No... you're getting in to VB style unclear meanings. With override, virtual, and new it makes it clear what you are doing. Without it you would have to get out the reflector to tell what a unit is doing.

Another reason for the warning but working is to make you aware that you have hidden a base class' member in case you did not intend to.

Re: virtual/new/override 80 80

Ahh, ok then - thanks for the heads up. Was just curious.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.