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.
-
- abstract class Abstractcls
- {
-
- 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.
-
- class Maths:Abstractcls
- {
- public override int Add(int a, int b, int c)
- {
- return a + b + c;
- }
- static void Main(string[] args)
- {
-
- 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.
-
- abstract class Abstractcls
- {
-
- public abstract int Add(int a, int b, int c);
-
- protected int x = 7;
-
- public abstract int val
- {
- get;
- }
- }
-
-
- class Maths:Abstractcls
- {
-
- public override int Add(int a, int b, int c)
- {
- return a + b + c;
- }
-
- public override int val
- {
- get
- {
- return x;
- }
- }
- static void Main(string[] args)
- {
-
- Maths _objMaths = new Maths();
- Console.WriteLine("Abstract function result={0}",_objMaths .Add (10,20,30));
-
- Console .WriteLine ("Abstract Properties:={0}",_objMaths.val );
- Console.ReadLine();
- }
- }
We can add simple methods to an abstract class. You cannot override simple methods in a derrived class.
-
- abstract class Abstractcls
- {
-
- public int minuse(int a, int b)
- {
- return a - b;
- }
- }
-
- class Maths:Abstractcls
- {
- static void Main(string[] args)
- {
-
- Maths _objMaths = new Maths();
- Console.WriteLine("Override String fun-{0}",_objMaths .minuse (10,4));
- }
- }
FAQ on Abstraction
Question 1: Can only abstract methods, properties or indexers be put in the abstract class?
Answer: No, you can write abstract methods and normal methods and properties for an abstract class, but you cannot override it (normal methods).
-
- abstract class Abstractcls
- {
-
- public int minuse(int a, int b)
- {
- return a - b;
- }
- }
-
- class Maths:Abstractcls
- {
- static void Main(string[] args)
- {
-
- Maths _objMaths = new Maths();
- Console.WriteLine("Override String fun-{0}",_objMaths .minuse (10,4));
- }
- }
Question 2: Can we use a Private or Protected access specifier with an abstract method?
Answer: No, you can use only public and the internal access specifier for an abstract class, method or property.
Question 3: Can we create an instance of an abstract class and if not then why?
Answer: We can't create an instance of an abstract class because an abstract class is incomplete. You only provide the signatures of properties, methods, events and indexers. An abstract class is always inherited by a simple class, as you saw in the above examples.
Question 4: Can we inherit an abstract base class in the derrived class?
Answer: Yes. You can do this. But you must override it in a derrived abstract class.
-
- abstract class abs1
- {
- public abstract int div(int a, int b);
- }
-
- abstract class Abstractcls:abs1
- {
- public abstract int Add(int a, int b, int c);
-
-
- public override int div(int a, int b)
- {
- return a / b;
- }
- }
-
- class Maths:Abstractcls
- {
-
- public override int Add(int a, int b, int c)
- {
- return a + b + c;
- }
-
- static void Main(string[] args)
- {
-
- Maths _objMaths = new Maths();
- Console.WriteLine("Abstract function result={0}",_objMaths .Add (10,20,30));
- Console.Write("Override int {0}=",_objMaths .div (10,2));
- Console.ReadLine();
- }
- }