Thursday 11 August 2011

Abstract Class in C Sharp

Abstract Class in C Sharp:

    A Class is declared with Abstract keyword, and which is can not be instantiated and they are frequently either partially implemented,
    or not at all implemented..
    It can be used as a Super Class or Base Class for other classes.
    Its Implementation is done only in sub classes
    A Class can inherit one abstract class and must override all its abstract methods etc.
Example:

public abstract class WashingMachine
{
   public WashingMachine()            //Implemented method
   {
      // Code to initialize the class goes here.
   }

   abstract public void Wash();            //Un Implemented method
   abstract public void Rinse(int loadSize);    //Un Implemented method
   abstract public long Spin(int speed);        //Un Implemented method
}

implementation of each abstract (Must Override) method in that class, and each implemented method must
receive the same number and type of arguments, and have the same return value,
as the method specified in the abstract class

public class MyWashingMachine : WashingMachine
{
   public MyWashingMachine()
   { 
      // Initialization code goes here.   
   }

   override public void Wash()
   {
      // Wash code goes here.
   }

   override public void Rinse(int loadSize)
   {
      // Rinse code goes here.
   }

   override public long Spin(int speed)
   {
      // Spin code goes here.
   }
}

No comments:

Post a Comment