473,769 Members | 6,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new versus override

Could someone explain the implications of the differences between using
'new' and 'override' on a member declaration, please.

I checked out MSDN but it's not exactly written for the beginner. E.g.
on Name Hiding, it starts with this: "The scope of an entity typically
encompasses more program text than the declaration space of the
entity." That whizzing sound you're hearing is it going over my head!

SSG

Nov 17 '05 #1
16 1746
Hello

Suppose you have some classes:

class A
{
public A() { }

public virtual void Method1()
{
System.Diagnost ics.Debug.Write Line("A.Method1 ()");
}
public virtual void Method2()
{
System.Diagnost ics.Debug.Write Line("A.Method2 ()");
}
}

class B : A
{
public B() { }

public override void Method1()
{
System.Diagnost ics.Debug.Write Line("B.Method1 ()");
}
public new void Method2()
{
System.Diagnost ics.Debug.Write Line("B.Method2 ()");
}
}
and then:

A a = new B();
a.Method1();
a.Method2();
So, you will see in output window:
B.Method1()
A.Method2()

So, if you override something, it will be accessible if you down casting to
base class. But if you creating a new method, you can not access it after
down casting to base class.

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"ssg3141592 6" <ne**********@g mail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Could someone explain the implications of the differences between using
'new' and 'override' on a member declaration, please.

I checked out MSDN but it's not exactly written for the beginner. E.g.
on Name Hiding, it starts with this: "The scope of an entity typically
encompasses more program text than the declaration space of the
entity." That whizzing sound you're hearing is it going over my head!

SSG

Nov 17 '05 #2
Hello

Suppose you have some classes:

class A
{
public A() { }

public virtual void Method1()
{
System.Diagnost ics.Debug.Write Line("A.Method1 ()");
}
public virtual void Method2()
{
System.Diagnost ics.Debug.Write Line("A.Method2 ()");
}
}

class B : A
{
public B() { }

public override void Method1()
{
System.Diagnost ics.Debug.Write Line("B.Method1 ()");
}
public new void Method2()
{
System.Diagnost ics.Debug.Write Line("B.Method2 ()");
}
}
and then:

A a = new B();
a.Method1();
a.Method2();
So, you will see in output window:
B.Method1()
A.Method2()

So, if you override something, it will be accessible if you down casting to
base class. But if you creating a new method, you can not access it after
down casting to base class.

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"ssg3141592 6" <ne**********@g mail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Could someone explain the implications of the differences between using
'new' and 'override' on a member declaration, please.

I checked out MSDN but it's not exactly written for the beginner. E.g.
on Name Hiding, it starts with this: "The scope of an entity typically
encompasses more program text than the declaration space of the
entity." That whizzing sound you're hearing is it going over my head!

SSG

Nov 17 '05 #3
I can be wrong
Both are keywords New is for object creation
and overrides if you want to overirde a method in inheritance

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/

Nov 17 '05 #4
I can be wrong
Both are keywords New is for object creation
and overrides if you want to overirde a method in inheritance

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/

Nov 17 '05 #5
I'm new to C# so forgive me if my question is trivial.

In the example given by Andrew above;

" So, if you override something, it will be accessible if you down
casting to
base class. But if you creating a new method, you can not access it
after
down casting to base class. "

Don't you not mean downcasting to *derived* class?

Am I right in thinking that if you have;

A a = new B;

then you *cannot* invoke B.method2() because it's declared "new" and is
thus not accessible from an A pointer. (pointer, I think is the wrong
term here, I'm coming at this from C++).

In order to do call it, you need to do something like
B b = new B;
b.method2();

Is that correct? Again, sorry if this is c# 101, I've read the tutorial
from softsteel and am just trying to ramp up.

thanks much

Graham

Nov 17 '05 #6
I'm new to C# so forgive me if my question is trivial.

In the example given by Andrew above;

" So, if you override something, it will be accessible if you down
casting to
base class. But if you creating a new method, you can not access it
after
down casting to base class. "

Don't you not mean downcasting to *derived* class?

Am I right in thinking that if you have;

A a = new B;

then you *cannot* invoke B.method2() because it's declared "new" and is
thus not accessible from an A pointer. (pointer, I think is the wrong
term here, I'm coming at this from C++).

In order to do call it, you need to do something like
B b = new B;
b.method2();

Is that correct? Again, sorry if this is c# 101, I've read the tutorial
from softsteel and am just trying to ramp up.

thanks much

Graham

Nov 17 '05 #7
Yes, you are right. If you need B's version of Method2, you need to do

B b = new B();

b.Method2();

It's not accessible through

A a = new B();

a.Method2(); // here is call to A's version of Method2


--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
<Gr**********@g mail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
I'm new to C# so forgive me if my question is trivial.

In the example given by Andrew above;

" So, if you override something, it will be accessible if you down
casting to
base class. But if you creating a new method, you can not access it
after
down casting to base class. "

Don't you not mean downcasting to *derived* class?

Am I right in thinking that if you have;

A a = new B;

then you *cannot* invoke B.method2() because it's declared "new" and is
thus not accessible from an A pointer. (pointer, I think is the wrong
term here, I'm coming at this from C++).

In order to do call it, you need to do something like
B b = new B;
b.method2();

Is that correct? Again, sorry if this is c# 101, I've read the tutorial
from softsteel and am just trying to ramp up.

thanks much

Graham

Nov 17 '05 #8
Yes, you are right. If you need B's version of Method2, you need to do

B b = new B();

b.Method2();

It's not accessible through

A a = new B();

a.Method2(); // here is call to A's version of Method2


--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
<Gr**********@g mail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
I'm new to C# so forgive me if my question is trivial.

In the example given by Andrew above;

" So, if you override something, it will be accessible if you down
casting to
base class. But if you creating a new method, you can not access it
after
down casting to base class. "

Don't you not mean downcasting to *derived* class?

Am I right in thinking that if you have;

A a = new B;

then you *cannot* invoke B.method2() because it's declared "new" and is
thus not accessible from an A pointer. (pointer, I think is the wrong
term here, I'm coming at this from C++).

In order to do call it, you need to do something like
B b = new B;
b.method2();

Is that correct? Again, sorry if this is c# 101, I've read the tutorial
from softsteel and am just trying to ramp up.

thanks much

Graham

Nov 17 '05 #9
Yes, a little bit wrong.

New is not only for object creation. It's also a modifier to class
methods/properties declaration.


--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
<sh**********@y ahoo.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
I can be wrong
Both are keywords New is for object creation
and overrides if you want to overirde a method in inheritance

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/

Nov 17 '05 #10

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

Similar topics

81
5184
by: sinister | last post by:
I wanted to spiff up my overly spartan homepage, and started using some CSS templates I found on a couple of weblogs. It looks fine in my browser (IE 6.0), but it doesn't print right. I tested the blogs, and one definitely didn't print right. Surveying the web, my impression is that CSS is very unreliable, because even updated browsers fail to implement the standards correctly. So should one just avoid CSS? Or is it OK if used...
15
122150
by: Gérard Talbot | last post by:
Hello all, I'd like to know and understand the difference between, say, <img src="/ImageFilename.png" width="123" height="456" alt=""> and <img src="/ImageFilename.png" style="width: 123px; height: 456px;" alt="">
2
2314
by: Fredrik Melin | last post by:
Hi, I have a vendor that requires me to send empty value, e.g. <OrderIDInfo /> problem is that my xslt need to add attributes, doing this <OrderIDInfo> <xsl:attribute name="orderID"> <xsl:value-of select="Root_Element/InvoiceHeader/Customer_PO_No" /> </xsl:attribute>
54
5862
by: Matt | last post by:
How do we define systems programs? when we say systems programming, does it necessary mean that the programs we write need to interact with hardware directly? For example, OS, compiler, kernel, drivers, network protocols, etc...? Couple years ago, yes, I understand this is definitely true. However, as the software applications become more and more complicated, some people try to argue that. Some people argue the definition of systems...
4
3213
by: Chris | last post by:
This is something that has been bugging me for some time. When exactly should I be using == instead of .Equals() ? I see a lot of code that performs object evaluation e.g. Is Object1 the same as Object2 by using == which works but shouldn't it be using .Equals()? e.g. Object1.Equals(Object2) I believe == should be using for simple type evaluation e.g. int a = 0;
3
2311
by: Neil Zanella | last post by:
Hello, I would like to ask the following question concerning the C# as operator. I would like to know whether the difference between using a C-style cast such as double x = 0; float y = (double) x; and using the as operator is simply that as works on references rather
0
238
by: ssg31415926 | last post by:
Could someone explain the implications of the differences between using 'new' and 'override' on a member declaration, please. I checked out MSDN but it's not exactly written for the beginner. E.g. on Name Hiding, it starts with this: "The scope of an entity typically encompasses more program text than the declaration space of the entity." That whizzing sound you're hearing is it going over my head! SSG
1
1344
by: robert d via AccessMonster.com | last post by:
I'm a fledgling developer and am just starting to get my app out to specific clients. I have been asked to bid on a project because my app already does over 90% of what the client wants. My question is with regards to pricing of the proposal. It seems like I should include a license fee for my app as it currently is in the pricing section of the proposal. If I don't do this, then they might claim that they paid for everything as...
0
3558
by: shumaker | last post by:
What's the difference between overriding OnFormClosing or simply wiring up my own event handler OnClosing that is not declared as override?
0
9590
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
9424
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
10223
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
10051
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...
0
8879
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
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 we have to send another system

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.