PC Review


Reply
Thread Tools Rate Thread

diff between virtual and abstract?

 
 
chandu
Guest
Posts: n/a
 
      8th Sep 2006
hello,

what is the difference to use the keyword virtual,abstract when we are
overriding the methods.instead of abstract shall we use virtual everywhere
when we need to override?
i am little confusing to use these keywords.

thaks..


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      8th Sep 2006
If a subclass *needs* to override a member (i.e. you can't specify a
meaningful body in the base class, as it can't be defined), then it should
be abstract in the base-class; if you just want to give the subclass the
*ability* to override a method (if they want) then it should be virtual. Any
abstract member forces the entire class to be abstract, which also means you
can't create actuall instances of the base class, but you can of the
superclass.

Does that make sense?

Marc


 
Reply With Quote
 
=?ISO-8859-2?Q?S=B3awomir_Pa=B6ko?=
Guest
Posts: n/a
 
      8th Sep 2006
chandu napisał(a):
> hello,
>
> what is the difference to use the keyword virtual,abstract when we are
> overriding the methods.instead of abstract shall we use virtual everywhere
> when we need to override?
> i am little confusing to use these keywords.
>
> thaks..
>
>

You must (sic!) override when you deriving from abstract class. You can
override metods (if for some reason you need to do something else) when
you deriving from virtual.

best,
Slawomir
 
Reply With Quote
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      8th Sep 2006
chandu,

In some cases you cannot provide a implementation for some methods, but
method have to be there because it is part of the class interface. You want
the programmers using the class to provide this implementation and you don't
want to let them instantiate objects of this class because without this
method it won't simply work. In this case you declare the method and the
class as abstract. Abstract classes cannot be instantiated because they are
not complete.

In other cases you can provide a default implementation of a method which is
good enough to make the control work even if not re-defined, but you want to
give the programmers using your class as a base class to provide their
better implementation. In this case you declare the method as virtual thus,
giving the progammers options whether to use the default or provide their
own eomplementation or if they which to use the default and tweak the
results.


--
HTH
Stoitcho Goutsev (100)

"chandu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> hello,
>
> what is the difference to use the keyword virtual,abstract when we are
> overriding the methods.instead of abstract shall we use virtual everywhere
> when we need to override?
> i am little confusing to use these keywords.
>
> thaks..
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About virtual and abstract method David Zha0 Microsoft C# .NET 4 13th Apr 2007 11:22 AM
virtual and abstract keywords Jose Fernandez Microsoft C# .NET 5 26th Oct 2006 10:43 AM
diff between abstract class and interface gordon Microsoft C# .NET 7 15th Sep 2006 10:40 AM
Re: abstract vs. virtual Miha Markic [MVP C#] Microsoft C# .NET 0 16th Mar 2004 07:22 AM
abstract vs. virtual Simon Cheng Microsoft C# .NET 0 16th Mar 2004 05:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:50 AM.