473,398 Members | 2,389 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,398 software developers and data experts.

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 9151
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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.