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

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 5177
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementation.
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**************@TK2MSFTNGP11.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****@steffsworld.ch> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementation.
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(int a, int b) {};
public void calculatePay(int a, int b, int c) {};
}

class Derived : Base
{
new public void calculatePay(int 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**************@TK2MSFTNGP09.phx.gbl...
"Stefan" <sh****@steffsworld.ch> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementation.
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(int a, int b) {};
public void calculatePay(int a, int b, int c) {};
}

class Derived : Base
{
new public void calculatePay(int 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**************@TK2MSFTNGP09.phx.gbl...
"Stefan" <sh****@steffsworld.ch> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Muscha

overriding = you use all the properties from the base class you just
overrides the mainimplementation.
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(int a, int b) {};
public void calculatePay(int a, int b, int c) {};
}

class Derived : Base
{
new public void calculatePay(int 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.SomeMethod, so the implementation in BaseClass will
still be used.

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

--
Jon Skeet - <sk***@pobox.com>
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*********@hotmail.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
Oops, sorry.

Just shows that it should be an error.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Jasper Kent <ja*********@hotmail.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #11

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

Similar topics

3
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...
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...
9
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...
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...
8
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)...
5
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
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...
3
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.