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

overriding non virtual methods, why bother with override at all then

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 differences between these two methods of overriding?

Thanks.
Nov 15 '05 #1
3 4769
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The difference is that when you make a method virtual, it's virtual all
the way up. When you *new* a non-virtual method, however, the base
class's version of the method is *hidden*, so up the inheritance
hierarchy, that method is still non-virtual. It is only virtual from the
class where you specify new virtual, down the inheritance chain.

news.microsoft.com wrote:

| 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 differences between these two methods of overriding?
|
| Thanks.
|
|
- --
Ray Hsieh (Djajadinata)
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/m/slwEwccQ4rWPgRAsLyAJ9WdUuOU0vOPq5eLc03piMrxVTPygCe Oou4
fAIjR2+mPM5rUBd+fqyYJnk=
=fN0g
-----END PGP SIGNATURE-----

Nov 15 '05 #2
There is a difference, and it lies in the behaviour of objects cast down to
their base classes. For example, let's say you have a class A which has a
virtual method. Class B inherits from A and overrides the virtual method.
If you cast down an object of type B to type A and execute the overridden
method, you will be executing the implementation of the method defined by
class B. However, if the method is not virtual, and it is hidden by a new
method in class B, execution of the method against an object of type B cast
down to type A will result in execution of the class A implementation of the
method. Run the following console application to verify this for yourself:

class ConsoleDemo
{
static void Main()
{
Console.WriteLine("Working with a B object:");
B b = new B();
b.ShowOverride();
b.ShowNew();

Console.WriteLine();
Console.WriteLine("Working with a B object cast down to an A
object:");
A a = (A)b;
a.ShowOverride();
a.ShowNew();

Console.ReadLine();
}
}

class A
{
public virtual void ShowOverride()
{
Console.WriteLine("A.ShowOverride()");
}

public void ShowNew()
{
Console.WriteLine("A.ShowNew()");
}
}

class B : A
{
public override void ShowOverride()
{
Console.WriteLine("B.ShowOverride()");
}

public new void ShowNew()
{
Console.WriteLine("B.ShowNew()");
}
}

"news.microsoft.com" <an********@discussions.microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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 differences between these two methods of overriding?

Thanks.

Nov 15 '05 #3
This should help:

Chapter 8 "Shadow Fields, Override Virtual Methods"*

http://www.geocities.com/jeff_louie/OOP/oop8.htm

Regards,
Jeff
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 differences between these two methods of overriding?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

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

Similar topics

2
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
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 ?
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...
3
by: Eric Chaves | last post by:
Hi fellows, According to the C# language specification (10.5.3), Every virtual method has a "most derived implementation" determined by a 3-step rule. If I invoke the virtual method from a normal...
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...
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: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.