473,408 Members | 1,729 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,408 software developers and data experts.

Overriding Methods and Virtual members in CS

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 bool Equals(object obj) {}

Another is using the override keyword, like:

public override bool Equals(object obj) {}

And my question is, what's the main diference of using one or another? Also,
I know about a keyword called virtual. I thought that the virtual keyword is
used to identify a method that can be overriden... I'm not sure about that,
I would like to know a better explain of virtual methods, cause I think that
using the new keyword, you can override any method in the inhereted class
(not sure about that)...

I wrote a struct where I override the Equals method, I did that first using
the override keyword, but the compiler throws a warning saying that I would
need to override the GetHashCode method too, why this? I would like to know
the diference about those declarations, and the real definition of virtual
methods!

Thanks guys!
Nov 15 '05 #1
4 2194
new bool Equals is only virtual from that point, wherase an overrise of an
exist virtual is virtual all the way up to the virtual definition
"Rafael Veronezi" <li******@terra.com.br> wrote in message
news:eB**************@TK2MSFTNGP11.phx.gbl...
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 bool Equals(object obj) {}

Another is using the override keyword, like:

public override bool Equals(object obj) {}

And my question is, what's the main diference of using one or another? Also, I know about a keyword called virtual. I thought that the virtual keyword is used to identify a method that can be overriden... I'm not sure about that, I would like to know a better explain of virtual methods, cause I think that using the new keyword, you can override any method in the inhereted class
(not sure about that)...

I wrote a struct where I override the Equals method, I did that first using the override keyword, but the compiler throws a warning saying that I would need to override the GetHashCode method too, why this? I would like to know the diference about those declarations, and the real definition of virtual
methods!

Thanks guys!

Nov 15 '05 #2
Rafael Veronezi <li******@terra.com.br> wrote:
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 bool Equals(object obj) {}
That doesn't override it - that hides it.
Another is using the override keyword, like:

public override bool Equals(object obj) {}

And my question is, what's the main diference of using one or another?
When you really override a method, it acts polymorphically. When you
hide it, it's like a new method. Here's an example to explain it:

using System;

class Base
{
public virtual void Foo()
{
Console.WriteLine ("Base.Foo");
}
}

class OverridesFoo : Base
{
public override void Foo()
{
Console.WriteLine ("OverridesFoo.Foo");
}
}

class HidesFoo : Base
{
public new void Foo()
{
Console.WriteLine ("HidesFoo.Foo");
}
}

class Test
{
static void Main()
{
Base x = new Base();
x.Foo();
x = new OverridesFoo();
x.Foo();
x = new HidesFoo();
x.Foo();

OverridesFoo y = new OverridesFoo();
y.Foo();

HidesFoo z = new HidesFoo();
z.Foo();
}
}

The output is:
Base.Foo
OverridesFoo.Foo
Base.Foo
OverridesFoo.Foo
HidesFoo.Foo

The first line is Base.Foo because the object in question just *is* a
Base instance.

The second line is OverridesFoo.Foo because the object in question is
an OverridesFoo instance, and it overrides the Base.Foo method.

The third line is Base.Foo because although the object in question is a
HidesFoo instance, the HidesFoo.Foo method doesn't override the
Base.Foo method - it's essentially a separate method which happens to
have the same name. The compiler only knows of the reference as being
of type Base.

The fourth line is OverridesFoo.Foo for hopefully obvious reasons.

The fifth line is HidesFoo.Foo because this time the compiler knows
that the reference is a HidesFoo, so it calls HidesFoo.Foo rather than
BaseFoo.Foo.
I wrote a struct where I override the Equals method, I did that first using
the override keyword, but the compiler throws a warning saying that I would
need to override the GetHashCode method too, why this? I would like to know
the diference about those declarations, and the real definition of virtual
methods!


When you override Equals you should also override GetHashCode so that
two equal objects return the same hash code - otherwise if you try to
use an instance of your struct as the key for a hashtable, it may not
work properly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
I think that I understood the difference between using new and override...
But I even don't know when I would use one or another?
Another question I was made was about the virtual keyword, I can only
override virtual methods?
Thanks!

"Jon Skeet [C# MVP]" <sk***@pobox.com> escreveu na mensagem
news:MP***********************@msnews.microsoft.co m...
Rafael Veronezi <li******@terra.com.br> wrote:
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 bool Equals(object obj) {}


That doesn't override it - that hides it.
Another is using the override keyword, like:

public override bool Equals(object obj) {}

And my question is, what's the main diference of using one or another?


When you really override a method, it acts polymorphically. When you
hide it, it's like a new method. Here's an example to explain it:

using System;

class Base
{
public virtual void Foo()
{
Console.WriteLine ("Base.Foo");
}
}

class OverridesFoo : Base
{
public override void Foo()
{
Console.WriteLine ("OverridesFoo.Foo");
}
}

class HidesFoo : Base
{
public new void Foo()
{
Console.WriteLine ("HidesFoo.Foo");
}
}

class Test
{
static void Main()
{
Base x = new Base();
x.Foo();
x = new OverridesFoo();
x.Foo();
x = new HidesFoo();
x.Foo();

OverridesFoo y = new OverridesFoo();
y.Foo();

HidesFoo z = new HidesFoo();
z.Foo();
}
}

The output is:
Base.Foo
OverridesFoo.Foo
Base.Foo
OverridesFoo.Foo
HidesFoo.Foo

The first line is Base.Foo because the object in question just *is* a
Base instance.

The second line is OverridesFoo.Foo because the object in question is
an OverridesFoo instance, and it overrides the Base.Foo method.

The third line is Base.Foo because although the object in question is a
HidesFoo instance, the HidesFoo.Foo method doesn't override the
Base.Foo method - it's essentially a separate method which happens to
have the same name. The compiler only knows of the reference as being
of type Base.

The fourth line is OverridesFoo.Foo for hopefully obvious reasons.

The fifth line is HidesFoo.Foo because this time the compiler knows
that the reference is a HidesFoo, so it calls HidesFoo.Foo rather than
BaseFoo.Foo.
I wrote a struct where I override the Equals method, I did that first using the override keyword, but the compiler throws a warning saying that I would need to override the GetHashCode method too, why this? I would like to know the diference about those declarations, and the real definition of virtual methods!


When you override Equals you should also override GetHashCode so that
two equal objects return the same hash code - otherwise if you try to
use an instance of your struct as the key for a hashtable, it may not
work properly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Rafael Veronezi <li******@terra.com.br> wrote:
I think that I understood the difference between using new and override...
But I even don't know when I would use one or another?
You'd almost *always* use override - hiding methods is almost always a
bad thing, because it's sometimes hard to follow what's going on.
Another question I was made was about the virtual keyword, I can only
override virtual methods?


Yes (well, those and abstract methods). The whole point of making a
method virtual is so that it can be overridden.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

3
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
5
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For...
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: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
4
by: Aamir Mahmood | last post by:
Hi all, I have a question. Can static members (methods and nested types) be made virtual in a class? So that they can be overridden in the child classes. For example: -------------------...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
10
by: Sebastian | last post by:
Hi, I'm confronted with a problem that seems not to be solvable. In general: How can I override an interface member of my base class and call the overridden method from my derived class? This...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.