new vs. override

Malfunction

Well-known member
Joined
Dec 8, 2003
Messages
203
Location
Berlin, Germany
I Dont know how to inherit methods correctly:

Class A is the baseclass of B. Both have an Init() method. Class Bs 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 Im 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. Youll 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

Youll see that new is primary used to mask names. Have a great chance understanding.

If you have more question dont fear to ask.
 
Back
Top