473,473 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is the difference between new and override

ad
We can use new or override to rewrite a method in an child class.
But what is the difference between new and override?
Nov 16 '05 #1
4 9152
Say you do this:
------------
DerivedType derivedInstance = new DerivedType();
BaseType baseVar = derivedInstance;
baseVar.CallMethod();
-------------

If the DerivedType.CallMethod() is defined as "new", the above code will
code the BaseType version. If it is defined as "override", it wil call the
DerivedType version.

If you are using inheritance to get polymorphism, you probably want
override.

Hope this helps,
Rachel

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
We can use new or override to rewrite a method in an child class.
But what is the difference between new and override?

Nov 16 '05 #2
"ad" <ad@wfes.tcc.edu.tw> wrote:
what is the difference between new and override?


http://www.yoda.arachsys.com/csharp/faq/#override.new

P.
Nov 16 '05 #3
> But what is the difference between new and override?

The other responses covered the technical details so here is just a bit of
intuition that might help.

You will almost always want "override" - it is rare to use "new".

override = more specific version of the same operation
new = new and unrelated method with same name and params as base method

If we put a method in a derived class that hides a base method, then we are
most likely being silly or careless. The compiler notices what is going on
and gives us a warning. The most common thing to do at this point is change
the name or param list of the derived method so there is no longer a
conflict. If we want to keep both methods the same, we need to acknowledge
that we really do want to hide the base method by adding "new".
Nov 16 '05 #4
You can also use new for non-virtual methods, events and properties(you
can't with override).

For example, we have a custom control library that is used as the basis for
our company's suite of applications. To deter the developers in other groups
from trying to change say, the background color of a control, we use "new"
to hide the underlying Control.BackgroundColor property So that if they set
the background color of one of our controls, we simply ignore it.

This isn't foolproof, though. If the end-developer wanted to get around it,
he/she could simply cast to the base class of the control and call the
BackgroundColor property that way. Of course, then we'd have to take him out
back and beat the $@#% out of him which is a much more effective deterrent.

Pete
Nov 16 '05 #5

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

Similar topics

86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
5
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. ...
4
by: Raja Chandrasekaran | last post by:
Hai friends, I really wonder, If the interface does not have any definition, Y do we need to use interface. You can then only we can use Multiple inheritance. I really cant understand, Just for...
8
by: pigeonrandle | last post by:
Hi, Please pity me, i am on a dial-up connection for the first time in 5 years :( ! Does anyone know how the resulting Graphics objects differ ...? What i really mean is can someone explain it...
2
by: gstark | last post by:
Hi, I have a difference between machines in the date formats they produce. This one web application has this code. Me.txtRxDate.Text = Now().ToShortDateString. On the development server, the...
1
by: siddh | last post by:
hiiiiiii i find 2 difference in new and override.......:-> 1 ::-> with new there is no need to be virtual, of the base class f/n......but in override its nessary to give base class f/n as...
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
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
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
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
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
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,...
1
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.