Overriding vs. New

  • Thread starter Thread starter muscha
  • Start date Start date
M

muscha

I don't get it. What's the main differences between overriding a base
class's methods vs. hiding them?


Thanks,

/m
 
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
 
Stefan said:
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
 
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 :-) ).
 
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
 
muscha said:
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.
 
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
 
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
 
Jasper Kent said:
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>
 
Back
Top