473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

virtual and override using inheritance

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\Anim al.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();
}
}
Nov 18 '07 #1
1 2046
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();
}
}

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
3
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any...
4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
2
by: junw2000 | last post by:
In the following code: #include <iostream> using namespace std; class V { public: int i; virtual void f() { cout << "V::f()" << endl;}
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.