Abstract categories, marked by the keyword abstract in the category definition, square measure usually accustomed outline a base category within the hierarchy. what is special concerning them, is that you just cannot create associate instance of them – if you try, you’ll get a compile error. Instead, you have to taxonomic category them, as tutored within the chapter on inheritance, and build associate instance of your taxonomic category. thus when does one would like Associate in Nursing abstract class? It really depends on what you are doing.

To be honest, you can go a protracted manner without having associate degree abstract category, but they’re nice for specific things, like frameworks, that is why you’ll find quite an little bit of abstract categories inside the .NET framework it self. A good rule of thumb is that the name actually makes really good sense – abstract classes are very often, if not always, used to describe something abstract, something that is more of a concept than a real thing.

In this example, we will create a base class for car  and then create a SafariCar class, which inherits from it, like this:

namespace AbstractClassesExample
{
 class Program
 {
 static void Main(string[] args)
 {
  SafariCar dog = new SafariCar ();
  Console.WriteLine(dog.SomeFeature());
  Console.ReadKey();
 }
 }

abstract class Car 
 {
 public virtual string SomeFeature()
 {
  return "Not much is known about this car!";
 }
 }

 class SafariCar : Car
 {

 }
}

Cannot create an instance of the abstract class or interface ‘AbstractClassesExample.Car’

Leave a Reply

Your email address will not be published. Required fields are marked *