473,800 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual and abstract keywords

Hello
I have been reading about the differences between virtual and abstract
keywords...

What i understand is that with VIRTUAL keyword, you can actually write the
whole method and override it if needed... with abstract, you just declare
the method name and must write the code in the inherited class. Am I right??

public class House{

public House(){}

public virtual string GetHouseName()
{
return "This is the House name Method"; // Virtual method implemented
}

public abstract string GetHouseNumber( ); // Abstract method not implemented
}

public class DogHouse : House
{
public DogHouse(){}

public override string GetHouseName()
{
return "This is the override GetHouseName method";
}

public override string GetHouseNumber( )
{
return "This is the number overrided method";
}
}
Oct 25 '06 #1
5 1771
Yes.

--
-Demetri
"Jose Fernandez" wrote:
Hello
I have been reading about the differences between virtual and abstract
keywords...

What i understand is that with VIRTUAL keyword, you can actually write the
whole method and override it if needed... with abstract, you just declare
the method name and must write the code in the inherited class. Am I right??

public class House{

public House(){}

public virtual string GetHouseName()
{
return "This is the House name Method"; // Virtual method implemented
}

public abstract string GetHouseNumber( ); // Abstract method not implemented
}

public class DogHouse : House
{
public DogHouse(){}

public override string GetHouseName()
{
return "This is the override GetHouseName method";
}

public override string GetHouseNumber( )
{
return "This is the number overrided method";
}
}
Oct 25 '06 #2
Hi,

I like to think of virtual as a "permission to override" and abstract as
an "obligation to override", though it's actually more an obligation to
implement.

BTW, override becomes overridden, not overrided ;-)

HTH,
Laurent

Jose Fernandez wrote:
Hello
I have been reading about the differences between virtual and abstract
keywords...

What i understand is that with VIRTUAL keyword, you can actually write the
whole method and override it if needed... with abstract, you just declare
the method name and must write the code in the inherited class. Am I right??

public class House{

public House(){}

public virtual string GetHouseName()
{
return "This is the House name Method"; // Virtual method implemented
}

public abstract string GetHouseNumber( ); // Abstract method not implemented
}

public class DogHouse : House
{
public DogHouse(){}

public override string GetHouseName()
{
return "This is the override GetHouseName method";
}

public override string GetHouseNumber( )
{
return "This is the number overrided method";
}
}


--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 25 '06 #3
jeje, Ok, overriden... i am improving my language too here... this works!!!

by the way, you can only have abstract methods in abstract class... Already
tested all the possible combinations and found out also that. You can have
virtual methods in abstracts class.

good wednesday
"Laurent Bugnion" <ga*********@bl uewin.chwrote in message
news:uT******** ********@TK2MSF TNGP02.phx.gbl. ..
Hi,

I like to think of virtual as a "permission to override" and abstract as
an "obligation to override", though it's actually more an obligation to
implement.

BTW, override becomes overridden, not overrided ;-)

HTH,
Laurent

Jose Fernandez wrote:
>Hello
I have been reading about the differences between virtual and abstract
keywords...

What i understand is that with VIRTUAL keyword, you can actually write
the whole method and override it if needed... with abstract, you just
declare the method name and must write the code in the inherited class.
Am I right??

public class House{

public House(){}

public virtual string GetHouseName()
{
return "This is the House name Method"; // Virtual method implemented
}

public abstract string GetHouseNumber( ); // Abstract method not
implemented
}

public class DogHouse : House
{
public DogHouse(){}

public override string GetHouseName()
{
return "This is the override GetHouseName method";
}

public override string GetHouseNumber( )
{
return "This is the number overrided method";
}
}


--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Oct 25 '06 #4
Hi,
Jose Fernandez wrote:
jeje, Ok, overriden... i am improving my language too here... this works!!!

by the way, you can only have abstract methods in abstract class... Already
tested all the possible combinations and found out also that.
The thing is, as soon as you have one abstract method in the class, the
class automatically becomes abstract too. How can you create an object
when one part of its behaviour is unknown? Since C# is a very strict
language, abstract classes must be marked with the abstract keyword,
that's why you get a compiler error when you omit it.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 25 '06 #5
In addition to what others said:

Not only methods can be abstract. Also properties, events and indexers can
be abstract.

"Jose Fernandez" <pp*****@hotmai l.comschrieb im Newsbeitrag
news:e$******** ********@TK2MSF TNGP04.phx.gbl. ..
Hello
I have been reading about the differences between virtual and abstract
keywords...

What i understand is that with VIRTUAL keyword, you can actually write the
whole method and override it if needed... with abstract, you just declare
the method name and must write the code in the inherited class. Am I
right??

public class House{

public House(){}

public virtual string GetHouseName()
{
return "This is the House name Method"; // Virtual method implemented
}

public abstract string GetHouseNumber( ); // Abstract method not
implemented
}

public class DogHouse : House
{
public DogHouse(){}

public override string GetHouseName()
{
return "This is the override GetHouseName method";
}

public override string GetHouseNumber( )
{
return "This is the number overrided method";
}
}

Oct 26 '06 #6

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

Similar topics

62
3400
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously bloats quickly for each new vtable needed. Execution slows down considerably as well. You can work around this by using interfaces referemnces which have a pointer to the object and a pointer to an external function lookup table. This technique...
37
4182
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
43
8736
by: Minti | last post by:
Hi there everybody, I was just wondering that too many people choose to use language like Java because of its architecture independence, this AI is achieved because Java is as such a platform indeed, the code is converted to a byte code and this byte code can be run on any machine which has
5
4536
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This I've to do by using override. It's also written, that's possible to "hide" the base class method by using the new key word. Because I've already written some C# code and I didn't know anything
6
3140
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm little confused. Am I deleting the right object when I call the virtual deconstructor? I was under impression when creating a class if I'm not specifically allocating memory, do not implement deconstructor and let the default take care of it...
3
38809
by: chandu | last post by:
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..
4
1790
by: Gert Kok | last post by:
The microsoft page http://msdn2.microsoft.com/en-us/library/9fkccyh4.aspx states: Remarks (nr 4) Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
4
6570
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this right? And, why not use "new" instead of using "virtual"? And the last question, what is the differences between a abstract method and a interface?
17
3552
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10273
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.