C# Abstract class and abstract method

The C# abstract class is a class which cannot be instantiated. You can’t create object of an abstract class by simply using new keyword. Abstract class only serves as a base class for its child classes. An abstract is used to express a design concept or an abstract idea. A typical example of an abstract class is a shape or a vehicle. A vehicle is an abstract idea, but a car, jeep or a truck is a concrete form of a vehicle. Consider following example to see how an abstract class can be created in C#.
CSHarp abstract Class, methods

Abstract Class

using System;
namespace CSharpTutorials {

    public abstract class Vehicle {
        public string name;
        public int price;
        public int model;
        public Vehicle(string vname, int vprice, int vmodel) {
            name = vname;
            price = vprice;
            model = vmodel;
        }
    }

    public class Car : Vehicle {
        public int seating_capacity;
        // creating constructor and calling base class constructor as well
        public Car(string vname, int vprice, int vmodel, int capacity) : base(vname, vprice, vmodel) {
            seating_capacity = capacity;
        }
        public void DisplayInfo() {
            Console.WriteLine("\nThe name of the car is:" + name);
            Console.WriteLine("The price of the car is:" + price);
            Console.WriteLine("The model of the car is:" + model);
            Console.WriteLine("The seating capacity of car is:" + seating_capacity);
        }
    }

    public class Truck : Vehicle {
        public int load_capacity;
        public Truck(string vname, int vprice, int vmodel, int load) : base(vname, vprice, vmodel) {
            this.load_capacity = load;
        }
        public void DisplayInfo() {
            Console.WriteLine("\nThe name of the Truck is:" + name);
            Console.WriteLine("The price of the Truck is:" + price);
            Console.WriteLine("The model of the Truck is:" + model);
            Console.WriteLine("The load capacity of Truck is:" + load_capacity + " tons.");
        }
    }
    class Tutorial {
        static void Main(string[] args)  {
            // Creating objects of car and truck class
            Car car = new Car("Toyota", 750000, 2010, 5);
            Truck Truck = new Truck("HINO", 4200000, 2005, 300);
            car.DisplayInfo();
            Truck.DisplayInfo();
            Console.ReadKey();
        }
    }
}

Download the code

The above code is similar to the one in the article on classes and objects. However, in this case, the vehicle class is declared abstract. To mark a class abstract, the keyword abstract is appended before the name of the class. Now, if you want to create object of Vehicle class in the Main method, you will see that you wont be able to do that since Vehicle class is abstract.

Abstract methods or abstract functions

Like abstract classes, abstract methods have no concrete implementation. The Abstract method is declared via abstract keyword. Abstract method do not have a body since the body is implemented by the classes that derive the method. Derived class must provide definition for all the abstract methods of the class which they are inheriting. This is done via override keyword. Consider following example.

using System;
namespace CSharpTutorials {
    public abstract class Vehicle {

        public string name;
        public int price;
        public int model;

        public  Vehicle(string vname, int vprice, int vmodel) {
            name = vname;
            price = vprice;
            model = vmodel;
        }
        public abstract void DisplayInfo();
    }

    public class Car : Vehicle {
        public int seating_capacity;
        // creating constructor and calling base class constructor as well
        public Car(string vname, int vprice, int vmodel, int capacity) : base(vname, vprice, vmodel) {
            seating_capacity = capacity;
        }
        public override void DisplayInfo() {
            Console.WriteLine("\nThe name of the car is:" + name);
            Console.WriteLine("The price of the car is:" + price);
            Console.WriteLine("The model of the car is:" + model);
            Console.WriteLine("The seating capacity of car is:" + seating_capacity);
        }
    }

    public class Truck : Vehicle {

        public int load_capacity;
        public Truck(string vname, int vprice, int vmodel, int load) : base(vname, vprice, vmodel) {
            this.load_capacity = load;
        }
        public override void DisplayInfo() {
            Console.WriteLine("\nThe name of the Truck is:" + name);
            Console.WriteLine("The price of the Truck is:" + price);
            Console.WriteLine("The model of the Truck is:" + model);
            Console.WriteLine("The load capacity of Truck is:" + load_capacity + " tons.");
        }
    }

    class Tutorial{
        static void Main(string[] args) {
            // Creating objects of car and truck class
            Car car = new Car("Toyota", 750000, 2010, 5);
            Truck Truck = new Truck("HINO", 4200000, 2005, 300);

            car.DisplayInfo();
            Truck.DisplayInfo();
            Console.ReadKey();
        }
    }    
}

Download the code

In the above code, the DisplayInfo method has been declared as abstract in the Vehicle class. The child classes Car and Truck override this method and provide implementation for it.

Further reading:

What is the difference between abstract method and a virtual method?

An abstract function does not have any functionality. It’s just a signature. You’re saying, any child class MUST give their own version of this method, however it’s too general to even try to implement in the parent class. A virtual function, on the other hand is “here’s the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality”. Stack overflow has some good discussion about this topic.

Also in cases you may need to define an abstract C# class without any abstract methods in it. Here is a discussion why we need abstract classes that do not have any abstract methods.

Are abstract structures possible in C#?

Abstract structures are not possible in C# since:

  • Structs in C# are sealed and live on the stack as opposed to classes that live on the heap
  • C# does not let structs inheritance.

Can C# abstract class be derived from Interface?

Yes. C# abstract classes can implement interfaces. Here is an example.

 

 

 

<<< Virtual Methods in C#Exception Handling in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .