473,399 Members | 4,177 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,399 software developers and data experts.

overriding and new on derived methods

Jon
Hello all,

I've just used the new keyword on an inherited method as the derived on is a
public int. What's the difference between the 2?

Thanks,

Jon
Nov 17 '05 #1
2 1122
Jon,

What are the 2?

The *new* keyword suppresses the worning that the compiler issues when you
hide a base class method. Using this keyword you tell the compiler that you
know what you are doing.

Consider the following snippet:

class Foo
{
public void Fun()
{
}
}

class Bar: Foo
{
public void Fun()
{
}
}

The moethod Fun is not decalred as a *virtual* method. It means that you are
hiding the base implementation. See the following code for example

Bar b = new Bar();
Foo f = b;

b.Foo() --> Calls Bar.Fun
f.Foo() --> calls Foo.Fun

Because this is error prone code the compilier warns you for the pitfall. If
this is exactly what you want and you are aware of the problems that it
might cause, just use the *new* keyword in the Bar's method declaration and
the compiler will suppress the warning.

HTH
Stoitcho Goutsev (100) [C# MVP]
"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
Hello all,

I've just used the new keyword on an inherited method as the derived on is
a
public int. What's the difference between the 2?

Thanks,

Jon

Nov 17 '05 #2
Jon <Jo*@discussions.microsoft.com> wrote:
I've just used the new keyword on an inherited method as the derived on is a
public int. What's the difference between the 2?


See http://www.pobox.com/~skeet/csharp/faq/#override.new

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

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

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
5
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For...
3
by: Eric Chaves | last post by:
Hi fellows, According to the C# language specification (10.5.3), Every virtual method has a "most derived implementation" determined by a 3-step rule. If I invoke the virtual method from a normal...
4
by: Nilesh | last post by:
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() {
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
4
by: drb | last post by:
Hi, I'm hoping this is a stupid question, so I apologize in advance. After creating WSDL files from scratch for a new web service, I used the WSDL.exe tool to generate C# stubs for both the...
10
by: Sebastian | last post by:
Hi, I'm confronted with a problem that seems not to be solvable. In general: How can I override an interface member of my base class and call the overridden method from my derived class? This...
12
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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,...
0
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...

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.