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

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 1752
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*********@bluewin.chwrote in message
news:uT****************@TK2MSFTNGP02.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*****@hotmail.comschrieb im Newsbeitrag
news:e$****************@TK2MSFTNGP04.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
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...
37
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
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...
5
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...
6
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...
3
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...
4
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...
4
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...
17
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;"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.