Saturday, 4 February 2012

What is Abstract Class you can say an Example?

-->
Yes, In .Net so many Abstract classes(base classes) are defined to Provide Same Basic Functionality to derived classes.

Examples : Array is Abstract Class [base class: it don't have any parent classes it is in the top most class]

Why Array class is defined as a abstract class?
                      Array is defined as Abstract class because this class contains properties,methods ...ect which are belongs to all arries of sclar/ premitive arrays.
Example1 :
            int[] intarr= new int[2];
            intarr[0] = 10;
            intarr[1] = 20;
            Console.WriteLine(intarr.Length);
                             -------------------------------Here is Length is a Property of Array class.
Example2 :
         string[] strarr = new string[3];
            strarr[0] = "anand";
            strarr[1] = "balu";
            strarr[2] = "charan";
            Console.WriteLine(strarr.Length);
                          --------------------------Here is Length is a Property of Array class.

Form Example1 : Integer Array -----intarrr.Lenght
From Example2: String Array-------strarr.Lenght
So...............Lenght---is common property to all types of arrays.
 Like If we define a boolean array there also we can get length of boolean array ------- so what we can understood throught this example is whatever array it is, means it can be interger array,or it   can be string array, or a boolean array -----ect have the same Property --lenght uses to know the elements in that array(int,string,bool,decimal..ect).
  class abstract Array
{
             -------------   ---------------
        public int Length { get; }---->  A 32-bit integer that represents the total number of elements in all the  dimensions of the System.Array.
}


Example Demo:
   -->
Using System.Collections;
    



Conclusion is:
int ,string,double,decimal,bool....ect all valuetypes uses is Array class(abstract class) as a base class to know the lenght of the their appropriate array classes. Code is Reused by all derived types in unified manner.

No comments:

Post a Comment