Abstract Classes in C#
The meaning of abstraction is, you don't need to explain everything. In our real life, we have vehicles such as cars, motorcycles and scooters. People only know how to drive these vehicles. It's not necessary to know how the vehicle runs or how petrol or diesel works in that vehicle.
So when we define an abstract we don't need to explain everything. It's a type of class, but you cannot create an instance (object) of it. In other words you cannot create an instance of the class. Now you might ask why do we not create an instance of an abstract class? The answer is that when we create or define an abstract class, it's not complete.
You must inherit this class and define its methods and properties on this new derived class.
Now I'll show you how all this is done. Have a look.
For the purposes of learning OOP concepts, Console applications are more convenient so I am using a Console Application.
- // File Name :- Abstractcls.cs
- abstract class Abstractcls
- {
- //An Example of how to define a Abstract Method.
- public abstract int Add(int a, int b, int c);
- }
One thing is to always remember that when you define an abstract function, always use your access specifier as public. Because private and protected are not able to use it.
- //FileName:- Maths.cs
- class Maths:Abstractcls
- {
- public override int Add(int a, int b, int c)
- {
- return a + b + c;
- }
- static void Main(string[] args)
- {
- //Creating objects of Maths class
- Maths _objMaths = new Maths();
- Console.WriteLine("Abstract function result={0}",_objMaths .Add (10,20,30));
- Console.ReadLine();
- }
- }
If you are a beginner in C# or do not understand console applications then here I want to explain Console.Writeline. This is used to output something. The meaning of {0} is you get the value of the first object in the argument list. So here {0} is the index of your first object. Here your object is _objMaths.
We can only inherit one abstract class for a class. In an interface we can do this, because inheritance is for multiple inheritance. In an abstract class, there is no rule for multiple inheritance.
Now we can learn how to implement properties in an abstract class, and override it in a derived class.
- // File Name:- Abstractcls.cs
- abstract class Abstractcls
- {
- //An Example of how to define a Abstract Method.
- public abstract int Add(int a, int b, int c);
- // x is protected variable
- protected int x = 7;
- //Define Abstract Property
- public abstract int val
- {
- get;
- }
- }
- // File Name:- Math.cs
- // Here Abstract class Inherit a derrive class Maths
- class Maths:Abstractcls
- {
- //Override Add Method
- public override int Add(int a, int b, int c)
- {
- return a + b + c;
- }
- //Override of val Property
- public override int val
- {
- get
- {
- return x;
- }
- }
- static void Main(string[] args)
- {
- //Creating objects of Maths class
- Maths _objMaths = new Maths();
- Console.WriteLine("Abstract function result={0}",_objMaths .Add (10,20,30));
- //getting the value of val Property
- Console .WriteLine ("Abstract Properties:={0}",_objMaths.val );
- Console.ReadLine();
- }
- }

Dr.Shahryar SultanPosted May 4, 2015, 1:30 PM
good
Vincent PaukgyiPosted Mar 19, 2014, 6:34 AM
Very Good.
Anil KumarPosted Apr 20, 2013, 2:49 AM
I think you must correct this paragraph -"When you create a normal class, you can easily define its methods, properties, indexers and events. But in an abstract class you can't define your methods, properties, indexers or events. All methods are defined in the derived class."