Getting started with Abstract class

Today, I will demonstrate you about Abstract class. It is a special type of class which cannot be instantiated and it behaves as parent class for other classes. The members of abstract class marked as abstract must be implemented by derived classes. Abstract class also contains concrete member methods.

Basically we create an abstract class because of it provides basic or default functionality as well as common functionality that multiple derived classes can share and override.

Let takes an example; a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

  1. using System;  
  2.   
  3. namespace ProgramCall  
  4. {  
  5.   
  6.     //Abstract class  
  7.     abstract class Shape1  
  8.     {  
  9.   
  10.         protected float R, L, B;  
  11.   
  12.         //Abstract methods can have only declarations  
  13.         public abstract float Area();  
  14.         public abstract float Circumference();  
  15.   
  16.     }  
  17.   
  18.   
  19.     class Rectangle1 : Shape1  
  20.     {  
  21.         public void GetLB()  
  22.         {  
  23.             Console.Write("Enter  Length  :  ");  
  24.   
  25.             L = float.Parse(Console.ReadLine());   
  26.   
  27.             Console.Write("Enter Breadth : ");  
  28.   
  29.             B = float.Parse(Console.ReadLine());  
  30.         }  
  31.   
  32.           
  33.         public override float Area()  
  34.         {  
  35.             return L * B;  
  36.         }  
  37.   
  38.         public override float Circumference()  
  39.         {  
  40.             return 2 * (L + B);  
  41.         }  
  42.   
  43.     }  
  44.   
  45.   
  46.     class Circle1 : Shape1  
  47.     {  
  48.   
  49.         public void GetRadius()  
  50.         {  
  51.   
  52.             Console.Write("Enter  Radius  :  ");  
  53.             R = float.Parse(Console.ReadLine());  
  54.         }  
  55.   
  56.         public override float Area()  
  57.         {  
  58.             return 3.14F * R * R;  
  59.         }  
  60.         public override float Circumference()  
  61.         {  
  62.             return 2 * 3.14F * R;  
  63.   
  64.         }  
  65.     }  
  66.     class MainClass  
  67.     {  
  68.         public static void Calculate(Shape1 S)  
  69.         {  
  70.   
  71.             Console.WriteLine("Area : {0}", S.Area());  
  72.             Console.WriteLine("Circumference : {0}", S.Circumference());  
  73.   
  74.         }  
  75.         static void Main()  
  76.         {  
  77.   
  78.             Rectangle1 R = new Rectangle1();   
  79.             R.GetLB();  
  80.             Calculate(R);  
  81.   
  82.             Console.WriteLine();  
  83.   
  84.             Circle1 C = new Circle1();   
  85.             C.GetRadius();  
  86.             Calculate(C);  
  87.   
  88.             Console.Read();  
  89.   
  90.         }  
  91.     }  
  92. }  
Abstract class has following features:
  1. An abstract class cannot be instantiated. It means we cannot create object of that class.

  2. It contains abstract members as well as non-abstract members [Concrete Member].

  3. It cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a   class to be inherited.

  4. We can inherit it from a class and one or more interfaces.

  5. It can have access modifiers like private, protected, internal with class members. But abstract members cannot have private access modifier.

  6. It can have instance variables (like constants and fields).

  7. It can have constructors and destructors.

So, today I explain you what is abstract class and its feature.

Hope you enjoy it.
Ebook Download
View all
Learn
View all