473,795 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding vs. New

I don't get it. What's the main differences between overriding a base
class's methods vs. hiding them?
Thanks,

/m
Nov 15 '05 #1
10 5210
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementat ion.
when you override a function the structure (public override void (int,int))
must be the same.

with new you can use the same method name as a method in the baseclass but
the structure can be different
and you use the properites, variables from the 'normal' class

hope this helps
"muscha" <mu****@no.spam .net> wrote in message
news:eZ******** ******@TK2MSFTN GP11.phx.gbl...
I don't get it. What's the main differences between overriding a base
class's methods vs. hiding them?
Thanks,

/m

Nov 15 '05 #2
"Stefan" <sh****@steffsw orld.ch> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementat ion.
when you override a function the structure (public override void (int,int)) must be the same.
with new you can use the same method name as a method in the baseclass but
the structure can be different
and you use the properites, variables from the 'normal' class


So with "new" all the method name of the base class rendered unusable?

Say for example:
class Base
{

public void calculatePay(in t a, int b) {};
public void calculatePay(in t a, int b, int c) {};
}

class Derived : Base
{
new public void calculatePay(in t a) {} // will make all calculatePay
methods in Base hidden?
}

Also with "new" keyword shouldn't you be able to use all the properties from
the base class as well if they are publicly available?
Thanks,

/m


Nov 15 '05 #3
with the 'new' keyword you say that your method uses the same name but does
something completely different to your base method.

sure you have access to properties and variables to the base class (made a
thinking mistake :-) ).
"muscha" <mu****@no.spam .net> wrote in message
news:Og******** ******@TK2MSFTN GP09.phx.gbl...
"Stefan" <sh****@steffsw orld.ch> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementat ion.
when you override a function the structure (public override void (int,int))
must be the same.
with new you can use the same method name as a method in the baseclass but the structure can be different
and you use the properites, variables from the 'normal' class


So with "new" all the method name of the base class rendered unusable?

Say for example:
class Base
{

public void calculatePay(in t a, int b) {};
public void calculatePay(in t a, int b, int c) {};
}

class Derived : Base
{
new public void calculatePay(in t a) {} // will make all calculatePay
methods in Base hidden?
}

Also with "new" keyword shouldn't you be able to use all the properties

from the base class as well if they are publicly available?
Thanks,

/m

Nov 15 '05 #4
> with the 'new' keyword you say that your method uses the same name but
does
something completely different to your base method.
I see. So in all sense, new and override is completely interchangeably
right? Just if you want to use override you need to have the "virtual"
keyword on the super class' method.
thanks,

/m

sure you have access to properties and variables to the base class (made a
thinking mistake :-) ).
"muscha" <mu****@no.spam .net> wrote in message
news:Og******** ******@TK2MSFTN GP09.phx.gbl...
"Stefan" <sh****@steffsw orld.ch> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementat ion.
when you override a function the structure (public override void

(int,int))
must be the same.
with new you can use the same method name as a method in the baseclass but the structure can be different
and you use the properites, variables from the 'normal' class


So with "new" all the method name of the base class rendered unusable?

Say for example:
class Base
{

public void calculatePay(in t a, int b) {};
public void calculatePay(in t a, int b, int c) {};
}

class Derived : Base
{
new public void calculatePay(in t a) {} // will make all calculatePay
methods in Base hidden?
}

Also with "new" keyword shouldn't you be able to use all the properties

from
the base class as well if they are publicly available?
Thanks,

/m


Nov 15 '05 #5
muscha <mu****@no.spam .net> wrote:
with the 'new' keyword you say that your method uses the same name but

does
something completely different to your base method.


I see. So in all sense, new and override is completely interchangeably
right?


Absolutely not. Consider that some client has some code like this:

BaseClass x = new DerivedClass();
x.SomeMethod();

If SomeMethod is virtual in BaseClass and overridden in DerivedClass,
the implementation in DerivedClass is called.

If SomeMethod is declared as *new* in DerivedClass, then it doesn't
override BaseClass.SomeM ethod, so the implementation in BaseClass will
still be used.

In both cases the compiler will emit a call to BaseClass.SomeM ethod,
but when the method is overridden the CLR can polymorphically run
DerivedClass.So meMethod if the instance it's being run on is an
instance of DerivedClass.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
> B obj1 = new D1 ();
B obj2 = new D2 ();
D2 obj3 = new D2 ();

obj1.func ();
obj2.func ();
obj3.func ();

The output is:

D1.func () called.
B.func () called.
D2.func () called.


Ah, got it I understand now. It is a bit confusing isn't it. I would have
expect once you declare a method in the derived class, the .Net virtual
machine should dynamically bind the correct method invocation.

Thanks

/m
Nov 15 '05 #7
Muscha... This is a FAQ:

Chapter 8 "Shadow Fields, Override Virtual Methods"*

http://www.geocities.com/jeff_louie/OOP/oop8.htm

Regards,
Jeff
I don't get it. What's the main differences between overriding a base

class's methods vs. hiding them?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #8
If you miss off override, you only get a warning, not an error, and you
get the behaviour as if you had put override (presuming the signatures
match).

In C++ and Java there is no override keyword, and in can lead to nasty
errors, viz:

class B
{
public:

virtual void Func ();
};

class D : public B
{
public:

void func ();
};

The misspelling of func () in the derived class means that it is
regarded as a brand new method and does not work polymorphically . In C#,
if you are in the habit of putting override, then you get an error
saying that there is nothing to override.

Regards,

Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #9
Jasper Kent <ja*********@ho tmail.com> wrote:
If you miss off override, you only get a warning, not an error, and you
get the behaviour as if you had put override (presuming the signatures
match).


No you don't - you get the behaviour as if you had put new (which is
why the warning is something like:

<quote>
Test.cs(33,17): warning CS0114: 'Derived.Baz()' hides inherited member
'Base.Baz()'. To make the current member override that implementation,
add the override keyword. Otherwise add the new keyword.
</quote>

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

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

Similar topics

3
3794
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for most operators, they are conspicuously absent for the logical "or" and "and". I'm guessing that the reason for this is that these operators short-circuit if their first operand answers the whole question? Would it be possible to allow...
3
4200
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 the documentation but it is rather hard to understand so please don't refer to it :) 3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
9
6362
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it didn't work. I discovered that this seemed to work: HTMLDocument.prototype.write= my_doc_write ; Why does HTMLDocument work here but not Document? Will this second
8
2267
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
2731
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 example, class base {
8
6741
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class) template<class X> void myfunc(X par1) { } (in derived class) template<class X>
5
2782
by: zero | last post by:
I'm having trouble with overriding methods in subclasses. I'll explain the problem using some code: class BaseClass { protected: void method2(); public: void method1();
15
24013
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a real world example of using virtual functions would be very much appreciated.
3
1557
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
10
105202
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
10214
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
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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...
1
4113
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
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.