Abstract Class
Abstract class means, We cannot create the object of this class or no object of this class can be instantiated. We can derive its sub classes which can be instantiated. An abstract class can contain either abstract or non-abstract members. Abstract class , marked by the keyword abstract.
Declaration of abstract class:
- abstract class DemoAbstract
- {
-
- }
Abstract Method :
An abstract method does not have any implement in abstract class. It has to be providing in its derived class and marked by the keyword abstract.
- abstract class DemoAbstract
- {
-
- public abstract void addTowNumber();
-
- public int AddThreeNumber(int a, int b, int c)
- {
-
- return a + b + c;
-
- }
-
- }
Note : An abstract calss does not mean that is should contain abstract member We can have an abstract class only with non-abstract member.
- abstract class DemoAbstract
- {
-
- public int AddThreeNumber(int a, int b, int c)
- {
-
- return a + b + c;
-
- }
-
- }
Example
- abstract class DemoAbstract
- {
-
-
-
- protected String _Myname;
-
- public abstract String Myname
- {
-
- get;
-
- set;
-
- }
-
-
- public abstract void addTowNumber(int a, int b);
-
- public int AddThreeNumber(int a, int b, int c)
- {
-
- return a + b + c;
-
- }
-
- }
-
- class DemoDerived : DemoAbstract
- {
-
-
-
- public override void addTowNumber(int a, int b)
- {
-
- Console.WriteLine(a + b);
-
- }
-
-
-
- public override String Myname
- {
-
- get
- {
-
- return _Myname;
-
- }
-
- set
- {
-
- _Myname = value;
-
- }
-
- }
-
- }
Note
- An abstract method and properties cannot be static.
- An abstract method and properties cannot be private.
- An abstract class cannot be sealed calss.
- An abstract method and properties cannot have modifier virtual.