473,499 Members | 1,862 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_call();
Base oDerived2 = new Derived();
oDerived2.test_call();

console.ReadLine();
}
}
class Base
{
public void test_call()
{
console.WriteLine("Base::test_call()");
}
}

class Derived : Base
{
public void test_call()
{
console.WriteLine("Derived::test_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 1695
Nilesh <dh*******@hotmail.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.com>
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*******@hotmail.com> wrote in message
news:ae**************************@posting.google.c om...
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_call();
Base oDerived2 = new Derived();
oDerived2.test_call();

console.ReadLine();
}
}
class Base
{
public void test_call()
{
console.WriteLine("Base::test_call()");
}
}

class Derived : Base
{
public void test_call()
{
console.WriteLine("Derived::test_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*******@hotmail.com> wrote in message
news:ae**************************@posting.google.c om...
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_call();
Base oDerived2 = new Derived();
oDerived2.test_call();

console.ReadLine();
}
}
class Base
{
public void test_call()
{
console.WriteLine("Base::test_call()");
}
}

class Derived : Base
{
public void test_call()
{
console.WriteLine("Derived::test_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
3772
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...
1
3826
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...
8
6712
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)...
3
7786
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...
10
5186
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
6832
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 {...
11
19430
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...
3
1518
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
3272
by: Shea Martin | last post by:
If I have a class Foo like this: class Foo { public: Foo() { mData = new char; } ~Foo()
0
7134
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,...
0
7180
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
7225
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...
1
6901
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...
1
4920
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
4605
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...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
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.