472,789 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 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 2159
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.