473,395 Members | 1,474 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,395 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 2261
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...
0
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...

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.