Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can abstract classes have implementation in c#?

Tags:

c#

oop

The compiler doesn't seem to mind it so far but I just wanted to double check whether I'm setting myself up for failure in any way by implementing certain methods in my abstract class.

like image 903
Brandon Moore Avatar asked Feb 03 '12 09:02

Brandon Moore


3 Answers

An abstract class usually has one or more abstract method. So yes it can have some method implemented. The goal is to force the user to implement these methods to have an object working. Sometimes abstract classes are used to provide a 'base' implementation of some interfaces, leaving the final user to specify just the key methods. You can also have an abstract class without any abstract method: in this case you are asserting you must derive from that class in order to use it.

like image 137
Felice Pollano Avatar answered Sep 21 '22 05:09

Felice Pollano


It's common to have some implementation in abstract classes.

If there is no implementation at all, consider using an interface instead of an abstract class.

like image 23
okrumnow Avatar answered Sep 23 '22 05:09

okrumnow


Perfectly fine to implement some methods and leave others abstract.

If all methods had to be abstract, you might as well use an interface for it.

like image 21
weston Avatar answered Sep 24 '22 05:09

weston