473,382 Members | 1,204 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,382 software developers and data experts.

Virtual Method Question

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.
Nov 16 '05 #1
4 1360

"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
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.

Thanks for any help with my understanding of this.

Nov 16 '05 #2
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.
Nov 16 '05 #3
> "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.
Nov 16 '05 #4
"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
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.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Raymond Lewallen | last post by:
I read this on this website page http://www.vbip.com/books/1861004915/chapter_4915_06.asp: Unlike many object-oriented languages, all methods in VB.NET are virtual. Now in BOL, Under...
4
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
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...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.