virtual and override using inheritance

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

I have three classes below. These are Animal as a base class and one derived
class called Cat.
The main in within class Tester.

If I compile the program as it is now I get the following
Warning 1 'Cat.MyTest()' hides inherited member 'Animal.MyTest()'. To make
the current member override that implementation, add the override keyword.
Otherwise add the new keyword.
F:\C#\ConsoleApplication1\ConsoleApplication1\Animal.cs 17 21
ConsoleApplication1

I understand the warning message.
I know it's wrong to have it as I have without new and without override.

But what is it that I can't do when I have it as I have now. I just want to
know.
Because I'm not using override here there must be something that I can't do
because of that.
If I have it as I have how does the compiler interprete it when not using
new and not using override.
Will the method MyTest be considered as a new method with the same name as
the base class.

//Tony


using System;

class Animal
{
private void Test()
{
Console.WriteLine("Test");
}

public virtual void MyTest()
{
Test();
}
}

class Cat : Animal
{
public void MyTest()
{
base.MyTest();
}
}

class Tester
{
static void Main(string[] args)
{
Cat myCat = new Cat();
myCat.MyTest();
}
}
 
Hi Tony,

Try the slightly modified code below. The difference is which method will be called, Animal.MyTest() or Cat.MyTest(). myCat.MyTest() outputs "Cat" since myCat is a Cat reference and calling myTest on a cat reference will run the Cat's MyTest. Likewise myAnimal runs Animal.MyTest() andoutputs "Animal". Now, myCatAnimal is pointing to a Cat object, but since it is an Animal reference, myCatAnimal.MyTest() will call Animal.MyTest and output "Animal". Commonly, inherited class objects will be sentto a shared method and treated as the base class. The last three linesshow that all the objects will be treated as an Animal object inside this method. What override does is force the code to call the actual object's MyTest() and not automatically use the currently referenced class' MyTest().

Try changing public void MyTest() to public override void MyTest() in the Cat class and run the program. Override can only be done on methods flagged as virtual, which is why Animal must use public virtual void MyTest(). public new void MyTest() just tells the compiler that you are aware of the danger of not overriding and effectively turns off the warning..

using System;

class Animal
{
private void Test()
{
Console.WriteLine("Animal");
}

public virtual void MyTest()
{
Test();
}
}

class Cat : Animal
{
public void MyTest()
{
Console.WriteLine("Cat");
}
}

class Tester
{
static void Main(string[] args)
{
Cat myCat = new Cat();
Animal myAnimal = new Animal();
Animal myCatAnimal = new Cat();

myCat.MyTest();
myAnimal.MyTest();
myCatAnimal.MyTest();


Console.WriteLine("---");

Method(myCat);
Method(myAnimal);
Method(myCatAnimal);
}

private static void Method(Animal animal)
{
animal.MyTest();
}
}
 
Back
Top