473,795 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why use 'new' in overriding?

I am confused about the purpose of 'new' in overriding.
Consider following example.

<code_snippet >

using console = System.Console;
public class TestClass
{
public static void Main()
{
Base oBase = new Base();
oBase.test_call ();

Derived oDerived = new Derived();
oDerived.test_c all();
Base oDerived2 = new Derived();
oDerived2.test_ call();

console.ReadLin e();
}
}
class Base
{
public void test_call()
{
console.WriteLi ne("Base::test_ call()");
}
}

class Derived : Base
{
public void test_call()
{
console.WriteLi ne("Derived::te st_call()");
}
}
</code_snippet>

In the derived classes' test call method, what is the use of adding
new? i.e.

new public void test_call()

Both the cases produce same result. So what is the purpose of 'new'?
Am i missing something too obvious?

Thanks in advance
Nilesh Dhakras
Nov 15 '05 #1
4 1706
Nilesh <dh*******@hotm ail.com> wrote:

<snip>
In the derived classes' test call method, what is the use of adding
new? i.e.

new public void test_call()

Both the cases produce same result. So what is the purpose of 'new'?
Am i missing something too obvious?


new is meant to indicate that you know full well that you're hiding a
method instead of overriding it, and that it is indeed what you want to
do.

It's rarely the right thing to do, however - most of the time you
should be overriding or using a different name.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
As Jon said, it's there to avoid confusion and help debugging your code.
It's not required, even though Visual Studio will mark the method and
claim "'new' is required" in the tooltip.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode with Nostalgia enabled
Nov 15 '05 #3
Hi,
the purpose of the new operator is to hide the base classes member with the
name you're using it on.
MSDN says:

Name hiding through inheritance takes one of the following forms:

a.. A constant, field, property, or type introduced in a class or struct
hides all base class members with the same name.
b.. A method introduced in a class or struct hides properties, fields, and
types, with the same name, in the base class. It also hides all base class
methods with the same signature. For more information, see 3.6 Signatures
and overloading.

--
Branimir Giurov
MSDE.NET, MCDBA
eAgility LLC
"Nilesh" <dh*******@hotm ail.com> wrote in message
news:ae******** *************** ***@posting.goo gle.com...
I am confused about the purpose of 'new' in overriding.
Consider following example.

<code_snippet >

using console = System.Console;
public class TestClass
{
public static void Main()
{
Base oBase = new Base();
oBase.test_call ();

Derived oDerived = new Derived();
oDerived.test_c all();
Base oDerived2 = new Derived();
oDerived2.test_ call();

console.ReadLin e();
}
}
class Base
{
public void test_call()
{
console.WriteLi ne("Base::test_ call()");
}
}

class Derived : Base
{
public void test_call()
{
console.WriteLi ne("Derived::te st_call()");
}
}
</code_snippet>

In the derived classes' test call method, what is the use of adding
new? i.e.

new public void test_call()

Both the cases produce same result. So what is the purpose of 'new'?
Am i missing something too obvious?

Thanks in advance
Nilesh Dhakras

Nov 15 '05 #4
100
Hi Nilesh
*new* doesn't change anything in your program it just tells the compiler
that you know what you are doing and the compiler won't report the warning
about hiding the base class method.

HTH
B\rgds
100

"Nilesh" <dh*******@hotm ail.com> wrote in message
news:ae******** *************** ***@posting.goo gle.com...
I am confused about the purpose of 'new' in overriding.
Consider following example.

<code_snippet >

using console = System.Console;
public class TestClass
{
public static void Main()
{
Base oBase = new Base();
oBase.test_call ();

Derived oDerived = new Derived();
oDerived.test_c all();
Base oDerived2 = new Derived();
oDerived2.test_ call();

console.ReadLin e();
}
}
class Base
{
public void test_call()
{
console.WriteLi ne("Base::test_ call()");
}
}

class Derived : Base
{
public void test_call()
{
console.WriteLi ne("Derived::te st_call()");
}
}
</code_snippet>

In the derived classes' test call method, what is the use of adding
new? i.e.

new public void test_call()

Both the cases produce same result. So what is the purpose of 'new'?
Am i missing something too obvious?

Thanks in advance
Nilesh Dhakras

Nov 15 '05 #5

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

Similar topics

3
3794
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for most operators, they are conspicuously absent for the logical "or" and "and". I'm guessing that the reason for this is that these operators short-circuit if their first operand answers the whole question? Would it be possible to allow...
1
3849
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
8
6741
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class) template<class X> void myfunc(X par1) { } (in derived class) template<class X>
3
7804
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
10
5210
by: muscha | last post by:
I don't get it. What's the main differences between overriding a base class's methods vs. hiding them? Thanks, /m
5
6852
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window { public void Draw() { Console.WriteLine("The WINDOW Draw method is running!");
11
19461
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the base class) then override(in the derived class), but fails to compare and contrast the two... I've read this (short snippet)three times and by the looks of it they both do exactly the same thing- allow you to derive from the base and save...
3
1557
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
10
3301
by: Shea Martin | last post by:
If I have a class Foo like this: class Foo { public: Foo() { mData = new char; } ~Foo()
0
9672
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
10437
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
10001
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
9042
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...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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
5437
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...
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.