473,770 Members | 6,736 Online
Bytes | Software Development & Data Engineering Community
+ 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 9172
Say you do this:
------------
DerivedType derivedInstance = new DerivedType();
BaseType baseVar = derivedInstance ;
baseVar.CallMet hod();
-------------

If the DerivedType.Cal lMethod() 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.ed u.tw> wrote in message
news:%2******** ********@TK2MSF TNGP11.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.ed u.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.Backgro undColor 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
7802
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: http://www.clagnut.com/blog/348/ I hope that's going to be a good discussion! Michael
3
17650
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
4
40030
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 deals with managed in one place, and code that deals with unmanaged in another place, but I can't seem to find anything that clearly explains what that means. I think if I used a Windows API function to optain a handle, that handle would be an...
5
5848
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. Thanks kai
4
20640
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 declararion y do we need to use interface. Anyhow the method name and definition ll be in the derived class. Instead of that we can do all code in the derived class itself right...? Then y these concept came. If anybody know, please explain me...
8
12777
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 to me please? A) protected static extern IntPtr GetWindowDC (IntPtr hWnd );
2
1247
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 browser displays the date as: 2/27/2007 When I goto the production server, the same browser displays the date as: 2/27/07. Where is this controled? It's not the "Regional and Langauge Settings", both machines are set the same.
1
1133
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 virtual..... 2 ::-> when appling base class object and derived class constructor o/p is 'base class' means like this::-> class base { public void display () { "base class" } }
2
2499
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 inheritance of the IEnumerable from the generic IEnumerable<T> to the generel IEnumerable and the program function just the same so no
0
9591
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
9425
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
10225
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
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7415
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
5312
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...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.