Cool way to skip implementation of an abstract method in an inherited class (Abstract Override keyword)

Making a class abstract helps us to abstract some base functionality in the base class and it will en-force all the inherited classes to give implementation for the abstract methods. Not in all cases we will be able to give implementation for all the methods. In these cases we give dummy implementation for those methods which is not relevant for the class. We can get rid of this through abstract override keyword in C#. For example abstract base class Mobile might have abstract (Must Override) methods like DisplayCamera, ActivateRadio, Init etc... Abstract methods like DisplayCamera, ActivateRadio needs implementation from the actual mobile class. We might have many implementation of SimpleMobile phone class. In those cases we might need a SimpleMobile base class which handles all the common operations of simple mobile phones, but need to delegate the implementation of some methods to the actual implementers. In these cases we can use Abstract Override keyword to dictate or delegate the implementation of some of the methods to the actual child implementers.  Making use of Abstract Override keyword makes the class again abstract.

public abstract class Mobile
{
      public abstract void Init();
      public abstract void ActivateRadio();
      public abstract void DisplayCamera();
}

public abstract class SimpleMobile : Mobile
{
       public override void Init()
      {
           // Implementation in child class.
       }

      // skipping the implementation.
      public abstract override void ActivateRadio();

      // skipping the implementation. Which is not relevant
       public abstract override void DisplayCamera();
}

 Note - Thanks for the comments.

Comments

  • Anonymous
    November 25, 2006
    Hi Ur code works fine...but the class which is implementing also needs to be abstract.....:)

  • Anonymous
    November 25, 2006
    I'm not sure I see the value here. As the previous commentator said, in order to contain abstract members, your derived class should be abstract too, which in that case you don't have to do anything. If you write SimpleMobile like this, it will compile, and you don't need the 'abstract override's until you write a non-abstract class in inheritance tree: public abstract class SimpleMobile : Mobile {      public override void Init()      {           // Implementation in child class.      } }

  • Anonymous
    November 25, 2006
    Yes... I agree to it... if we use abstract override then the class becomes abstract. The intention is that the class which inherits the base can force their implementers to provide implementation to its base class function. Abstract override keyword delegates the implementation to its child class. Example does not match the actual usage. Let me re-phrase.. thanks…