new vs. override

Malfunction

Junior Contributor
Joined
Dec 8, 2003
Messages
203
Location
Berlin, Germany
I Don't know how to inherit methods correctly:

Class A is the baseclass of B. Both have an Init() method. Class B's init method should only add functionality to the base class' init method.

In class B I can either write

new protected void Init() {
base.Init();
...do some more init stuff
}

or (given that Init() in class A is declared virtual)

protected override void Init() {
base.Init();
...do some more init stuff
}

I know I'm messing up stuff ... plz tell me the correct usage of new and override thx :D
 
Do a virtual class that will cover A and B. And inherit A from the virtual and inherit B from A. You'll be able to use the override thingy (i think so ! :p). But in your case... you better use new.

The main difference in this... is that override actually override it while new is only masking it (that what VS.NET seems to make me understand at least).

You can read more about :

new : http://msdn.microsoft.com/library/en-us/csref/html/vclrfNewOpPG.asp
override: http://msdn.microsoft.com/library/en-us/csref/html/vclrfOverridePG.asp

You'll see that new is primary used to mask names. Have a great chance understanding.

If you have more question don't fear to ask.
 
Back
Top