473,608 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4789
-----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/slwEwccQ4rWPgRA sLyAJ9WdUuOU0vO Pq5eLc03piMrxVT PygCeOou4
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.WriteLi ne("Working with a B object:");
B b = new B();
b.ShowOverride( );
b.ShowNew();

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

Console.ReadLin e();
}
}

class A
{
public virtual void ShowOverride()
{
Console.WriteLi ne("A.ShowOverr ide()");
}

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

class B : A
{
public override void ShowOverride()
{
Console.WriteLi ne("B.ShowOverr ide()");
}

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

"news.microsoft .com" <an********@dis cussions.micros oft.com> wrote in message
news:%2******** *******@TK2MSFT NGP11.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
3764
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
2256
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
2215
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 bool Equals(object obj) {} Another is using the override keyword, like: public override bool Equals(object obj) {}
14
12114
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
3
1538
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 variable, everything is ok. However if I call it inside a non-virtual method from the base class, that use's the *this*pointer to actually invoke the virtual method, then those rules doesn't seems to be respected. In fact, the behavior the code below...
4
1924
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 the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
17
2906
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 when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
6
27791
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 was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if it has arguments. (example ** assuming the Person class's constructor has (string FirstName) as...
10
105171
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
8087
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8025
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8509
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8365
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5499
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3993
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2493
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1620
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.