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

New vs Override

Hi
Can someone help me straighten out my train of thought... I want to
know what actually goes on behind the scene..step by step.

Here is the code:

-----------------
using System;

namespace Test
{
public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
}
class MyDerived : MyBase
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;

System.Console.WriteLine(mD.Meth1()); // Displays: MyDerived-Meth1
System.Console.WriteLine(mB.Meth1()); // Displays: MyDerived-Meth1
System.Console.WriteLine(mB.Meth2()); // Displays: MyBase-Meth2
System.Console.WriteLine(mD.Meth2()); // Displays: MyDerived-Meth2
}
}

}

-------
Here is what I think is currently happening (please correct me if I'm
wrong).
Question 1) When mD.Meth1() is called, it checks the memory locaton in
the heap contained in mD and goes to that location. Now, it finds the
MyDerived object.

**HERE IS WHERE MY CONFUSION BEGINS**
What does it actually do? Does it do the following:
PATH 1 - Go into MyDerived and realize it's inheriting MyBase. It then
goes to MyBase and finds Meth1() and notice it to be a virtual
function. It comes back out of MyBase and goes back to MyDerived and
notice that the function has been correclty overriden and uses that
function.

OR

PATH 2 - Go into MyDerived and does not even bother going into MyBase.
It notices that it has that function in it and it's overridding the
base function and calls it.

Quesiton 2) When mB.Meth2() is called, since the object really being
used is still MyDerived, how come MyDerived.Meth2() isn't called? The
new function obviously hides it, and the object being worked on is
MyDervived...not MyBase.

Why does it take PATH 1 and go into MyBase and calls that function when
the function has a keyword NEW which hides it?

Please help!!!

Dec 15 '05 #1
3 2258
methodios wrote:
Here is what I think is currently happening (please correct me if I'm
wrong).
Question 1) When mD.Meth1() is called, it checks the memory locaton in
the heap contained in mD and goes to that location. Now, it finds the
MyDerived object.

**HERE IS WHERE MY CONFUSION BEGINS**
What does it actually do? Does it do the following:
PATH 1 - Go into MyDerived and realize it's inheriting MyBase. It then
goes to MyBase and finds Meth1() and notice it to be a virtual
function. It comes back out of MyBase and goes back to MyDerived and
notice that the function has been correclty overriden and uses that
function.

OR

PATH 2 - Go into MyDerived and does not even bother going into MyBase.
It notices that it has that function in it and it's overridding the
base function and calls it.
The latter.

Every class has a table with slots for each virtual method in the
class, where each slot points to the implementation of that method.
When you override a method in a derived class, the new implementation
simply replaces the old one in the derived class's virtual method
table. Calling a virtual method means looking up the virtual method
table, finding the slot corresponding to the method you want, and
jumping to the address found there. The type of the object doesn't
really even matter, only the addresses stored in the table.

We can imagine the table for MyBase looking like this (ignoring the
virtual methods inherited from System.Object, like ToString):

"Meth1" -> MyBase.Meth1
"Meth2" -> MyBase.Meth2

And for MyDerived:

"Meth1" -> MyDerived.Meth1
"Meth2" -> MyBase.Meth2

Since you used the "new" keyword, the compiler knows that
MyDerived.Meth2 isn't the same method as MyBase.Meth2, so mD.Meth2()
results in a call to the new method, not the old one. However, it can
only use that information when you call through a variable of the
derived type. When you call through a MyBase variable, the compiler can
only see the Meth2 defined in MyBase.
Quesiton 2) When mB.Meth2() is called, since the object really being
used is still MyDerived, how come MyDerived.Meth2() isn't called? The
new function obviously hides it, and the object being worked on is
MyDervived...not MyBase.


This is precisely the difference between overriding and hiding.
MyDerived.Meth2 isn't actually related to MyBase.Meth2; they just
happen to have the same name. At compile time, the method call is bound
to the derived version, not the base version, because of the type of
the variable you're using to call the method.

Jesse

Dec 15 '05 #2
"Jesse McGrew" <jm*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
methodios wrote:
Here is what I think is currently happening (please correct me if I'm
wrong).
Question 1) When mD.Meth1() is called, it checks the memory locaton in
the heap contained in mD and goes to that location. Now, it finds the
MyDerived object.

**HERE IS WHERE MY CONFUSION BEGINS**
What does it actually do? Does it do the following:
PATH 1 - Go into MyDerived and realize it's inheriting MyBase. It then
goes to MyBase and finds Meth1() and notice it to be a virtual
function. It comes back out of MyBase and goes back to MyDerived and
notice that the function has been correclty overriden and uses that
function.

OR

PATH 2 - Go into MyDerived and does not even bother going into MyBase.
It notices that it has that function in it and it's overridding the
base function and calls it.


The latter.

Every class has a table with slots for each virtual method in the
class, where each slot points to the implementation of that method.
When you override a method in a derived class, the new implementation
simply replaces the old one in the derived class's virtual method
table. Calling a virtual method means looking up the virtual method
table, finding the slot corresponding to the method you want, and
jumping to the address found there. The type of the object doesn't
really even matter, only the addresses stored in the table.

We can imagine the table for MyBase looking like this (ignoring the
virtual methods inherited from System.Object, like ToString):

"Meth1" -> MyBase.Meth1
"Meth2" -> MyBase.Meth2

And for MyDerived:

"Meth1" -> MyDerived.Meth1
"Meth2" -> MyBase.Meth2

Since you used the "new" keyword, the compiler knows that
MyDerived.Meth2 isn't the same method as MyBase.Meth2, so mD.Meth2()
results in a call to the new method, not the old one. However, it can
only use that information when you call through a variable of the
derived type. When you call through a MyBase variable, the compiler can
only see the Meth2 defined in MyBase.
Quesiton 2) When mB.Meth2() is called, since the object really being
used is still MyDerived, how come MyDerived.Meth2() isn't called? The
new function obviously hides it, and the object being worked on is
MyDervived...not MyBase.


This is precisely the difference between overriding and hiding.
MyDerived.Meth2 isn't actually related to MyBase.Meth2; they just
happen to have the same name. At compile time, the method call is bound
to the derived version, not the base version, because of the type of
the variable you're using to call the method.

Jesse


Well written Jesse.

Also just to add:

A reintroduced call is fast as the compiler can go the type od the reference
and simply call the method.

A overridden call is slower as the runtime must access your object, access
its type (VTable) and call the correct method.

- Michael S

ps.
I still think that Delphis keyword 'reintroduce' is better than 'new'
Dec 15 '05 #3
In addition to what others have said notice that you get the same
behavior without the 'new' modifier. You do get a warning from the
compiler telling you that a derived class is hiding an inherited
member. The 'new' modifier is a way to explicitly tell the compiler
that hiding is indeed what you intended. But, it doesn't actually
change the execution of code in any way. That's a good thing. Let me
give you a practical example to show you why.

Consider the following code.

// This is provided by the Wizbang Widget Company
public class Widget
{
public virtual void Foo() { }
public virtual void Bar() { }
}

// You decide to extend the Widget class to make it better.
public class BetterWidget : Widget
{
public void Initialize()
{
// Code to initialize the class goes.
}
}

Later, the Wizbang Widget Company decides that they need to add a
mechanism for initializing the Widget so naturally they add an
Initialize method.

public class Widget
{
public virtual void Foo() { }
public virtual void Bar() { }
public void Initialize() { }
}

Can you believe that. They added a method to their Widget class with
the same name as an existing method in your derived class! What are
the odds of that?!

In this case all existing BetterWidget references will still use the
BetterWidget.Initialize implementation and any new uses of the
Initalize method on a Widget reference will use the Widget.Initialize
method. You will get a compiler warning, but everything works as
expected without any modifications to the code.

Can you see why it would have been worse had the C# compiler generated
an error or changed the way the methods were called?

Brian

methodios wrote:
Hi
Can someone help me straighten out my train of thought... I want to
know what actually goes on behind the scene..step by step.

Here is the code:

-----------------
using System;

namespace Test
{
public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
}
class MyDerived : MyBase
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;

System.Console.WriteLine(mD.Meth1()); // Displays: MyDerived-Meth1
System.Console.WriteLine(mB.Meth1()); // Displays: MyDerived-Meth1
System.Console.WriteLine(mB.Meth2()); // Displays: MyBase-Meth2
System.Console.WriteLine(mD.Meth2()); // Displays: MyDerived-Meth2
}
}

}

-------
Here is what I think is currently happening (please correct me if I'm
wrong).
Question 1) When mD.Meth1() is called, it checks the memory locaton in
the heap contained in mD and goes to that location. Now, it finds the
MyDerived object.

**HERE IS WHERE MY CONFUSION BEGINS**
What does it actually do? Does it do the following:
PATH 1 - Go into MyDerived and realize it's inheriting MyBase. It then
goes to MyBase and finds Meth1() and notice it to be a virtual
function. It comes back out of MyBase and goes back to MyDerived and
notice that the function has been correclty overriden and uses that
function.

OR

PATH 2 - Go into MyDerived and does not even bother going into MyBase.
It notices that it has that function in it and it's overridding the
base function and calls it.

Quesiton 2) When mB.Meth2() is called, since the object really being
used is still MyDerived, how come MyDerived.Meth2() isn't called? The
new function obviously hides it, and the object being worked on is
MyDervived...not MyBase.

Why does it take PATH 1 and go into MyBase and calls that function when
the function has a keyword NEW which hides it?

Please help!!!


Dec 15 '05 #4

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

Similar topics

3
by: Woodmon | last post by:
Example of my CSS follows: <style type="text/css" media="screen"> BODY { color: white } A:link { color: 66CCFF; } A:visited { color: CC66FF; } A:active { color: CC66FF; } A:hover {...
0
by: Craig Schneider | last post by:
// Is there any way to override the XML Serialization of the following SimpleClass // to turn off the DefaultValue of a boolean? Sure, I can override the DefaultValue from // true to false, but...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
2
by: Flip | last post by:
In java, the default for methods is override. In c# that is not the case. This talks about two classes, the base class and the overriding class. What happens when you have a third class in the...
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
2
by: Adriano Coser | last post by:
Hello. After I converted my .net code to the new VC2005 syntax I started to get C4490 on my ExpandableObjectConverter subclass overrides. The GetProperties method is no longer called by the...
8
by: bdeviled | last post by:
I am deploying to a web environment that uses load balancing and to insure that sessions persist across servers, the environment uses SQL to manage sessions. The machine.config file determines how...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...

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.