Virtual Method Question

P

Phill

I read in one of Jesse Liberty's books that:

"Because ToString() is a virtual method in the base class Object, it
is guaranteed to be available in every derived class." (P.179
Programming C#)

My question is, wouldn't the ToString() method be guaranteed to be
available to derived classes even if it had not been declared as
virtual?

I thought virtual just made it so the derived class's version of the
ToString() method would be called instead of the Object's version when
the overide keyword was used in the derived method's declaration.

How does NOT using virtual NOT guarantee the method's availability?

Also, anyone understand why the new/derived keywords are really
necessary? I mean like in C++, doesn't dynamic binding take place no
matter what, once the method is declared virtual in a base class?

Thanks for any help with my understanding of this.
 
D

Daniel O'Connell [C# MVP]

Phill said:
I read in one of Jesse Liberty's books that:

"Because ToString() is a virtual method in the base class Object, it
is guaranteed to be available in every derived class." (P.179
Programming C#)

My question is, wouldn't the ToString() method be guaranteed to be
available to derived classes even if it had not been declared as
virtual?
Yes, I think it matters more that it is a member of object, not that it is
virtual. Without having more of the text I couldn't say for sure or not.

Also, anyone understand why the new/derived keywords are really
necessary? I mean like in C++, doesn't dynamic binding take place no
matter what, once the method is declared virtual in a base class?

The point of epxlicit virtuality and overriding is that developers *have* to
be aware of the virtuality. You won't get bitten by a base class adding a
virtual method after you've written your implementation, etc. I prefer
explicitness, but many don't seem to.
 
K

kw

"Because ToString() is a virtual method in the base class Object, it
is guaranteed to be available in every derived class." (P.179
Programming C#)

My question is, wouldn't the ToString() method be guaranteed to be
available to derived classes even if it had not been declared as
virtual?

Yes, but then you couln't override it.
I thought virtual just made it so the derived class's version of the
ToString() method would be called instead of the Object's version when
the overide keyword was used in the derived method's declaration.

That's right.
How does NOT using virtual NOT guarantee the method's availability?

NOT using virtual only guarantees that it can't be overriden.
Also, anyone understand why the new/derived keywords are really
necessary? I mean like in C++, doesn't dynamic binding take place no
matter what, once the method is declared virtual in a base class?

These keywords are interpreted by looking at the most derived object.
Neccesity of usage depends on your application.
 
S

Sean Malloy

"Because ToString() is a virtual method in the base class Object, it
is guaranteed to be available in every derived class." (P.179
Programming C#)

ToString would be available to every subclass whether it is virtual or not.

The problem here is the english used to describe the situation.

I think what Jesse meant to say was "Because ToString() is a virtual method
in the base class Object, it is overridable in every derived class."

or something along those lines.
 
M

Mike Schilling

Phill said:
Also, anyone understand why the new/derived keywords are really
necessary? I mean like in C++, doesn't dynamic binding take place no
matter what, once the method is declared virtual in a base class?

They're not necessary, per se; they're used to avoid *accidentally*
overriding a method. In C++, if you add a method

virtual void foo()

to a class, any subclass that already had a method with this signature now
overrides the base class method, quite possibly unintentionally No compiler
errors or warnings will result; since the same syntax is used to introduce a
virtual method and override an existing virtual method, the compiler can't
tell that anything is wrong. This makes it somewhat dangerous to add a new
virtual method to a class which is widely derived from, particularly in a
class library that's used at other sites (like the .NET framework.)

In C#, a subclass that defines either

virtual void foo() or new void foo()

will *not* override the base class method (since it uses the virtual keyword
rather than override). The compiler will understand that they are entirely
different methods that happened to be spelled the same way, and most calls
to them will do the right thing. (I can picture some problems with
reflection, but that's an unusual case.) Also, the next time the subclass
is compiled, you'll see a warning about the name clash. whicxh may inspire
you to rename one or the other.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top