Abstract Class in C#

Abstract Class in C#

“An abstract is implement that the class is incomplete and can’t be use directly, to use an abstract class other class can derived.”

An abstract class contains abstract method which can be implemented by the

  • You can’t create instance of a abstract class.
  • You can’t override the method of the abstract class in the base class.
  • You can’t declare abstract method outside abstract class.
  • You can’t declare abstract class as a sealed.
  • A class which is derived from the abstract class must override all the method inside the abstract class.
  • If a derived class doesn’t implement all of the abstract method in the base class, then the derived class must also be specified by abstract.
  • Abstract method is method without any body.

Example

  1. using System;  
  2. abstract class Animal  
  3. {  
  4.    public abstract void FoodHabits();  
  5. }  
  6. class Carnivorous:Animal  
  7. {  
  8.    public override void FoodHabits()  
  9.    {  
  10.       Console.WriteLine("The Carnivorous Animal Eat Only Meat");  
  11.    }  
  12. }  
  13. class Herbivorous:Animal  
  14. {  
  15.    public override void FoodHabits()  
  16.    {  
  17.       Console.WriteLine("The Harbivorous Animal Eat Only Plants");  
  18.    }  
  19. }  
  20. class Implement  
  21. {  
  22.    public static void Main()  
  23.    {  
  24.       Carnivorous cn = new Carnivorous();  
  25.       Herbivorous hb = new Herbivorous();  
  26.       cn.FoodHabits();  
  27.       hb.FoodHabits();  
  28.       Console.ReadLine();  
  29.    }  
  30. }  
See the output below

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now