Revision: 49759
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 29, 2011 08:36 by BrentS
Initial Code
/* * An abstract class can contain some common functionality, such as the * "YouDontHaveToWriteThisFunctionAgain" method. This function can be called * from the class that inherits this AbstractTest class using the base keyword: * base.YouDontHaveToWriteThisFunctionAgain() * Also the class that inherits this AbstractTest class must implement the other * functions marked "abstract". That is, the inheriting class must define its * own function for those abstract members. */ public abstract class AbstractTestBase { private const int m_multiplier = 2; // The class inheriting AbstractTestBase can call this function by using // the base keyword: base.YouDontHaveToWriteThisFunctionAgain() // If you don't intend to have a common, reusable function like this, then // you shouldn't use an abstract class. Instead, create an interface class. // NOTE: Use the "public" access modifier instead of "protected" if you want // to allow the implimenting class to have direct access to this function. protected int YouDontHaveToWriteThisFunctionAgain(int value) { return value * m_multiplier; } // The class inheriting this must implement the following function. // We don't care how it is implemented, just that it is implemented. public abstract void YouMustImplementThis(); } // Now you can inherit from the AbstractTestBase class and use it like this: public class TestAbstractClass : AbstractTestBase { // The key feature of an abstract class is that you can re-use code from the base: public int WrapAndUseAnAbstractMethod(int value) { return base.YouDontHaveToWriteThisFunctionAgain(value); } public override void YouMustImplementThis() { // Add your code here for this function! throw new NotImplementedException("You have to write code here"); } }
Initial URL
Initial Description
Initial Title
AbstractClassExample
Initial Tags
class, c#
Initial Language
C#