By 22/07/2015 2 Comments

C# Abstract Classes And Abstract Methods

  • In C# a base class can defines incomplete or unimplemented methods and that class make sure that methods must be completed or implemented by its derived classes
  • Incomplete methods in the base class are called “abstract methods” and is marked with keyword “abstract”, abstract methods have only declaration such as method name, its return type and parameter list. These methods do not have their body and any code/logic.
  • If a class has one or more abstract methods that class must be declare as “abstract class” through specify a keyword “abstract”.
  • The object reference of an abstract class can be created but its object cannot be created.
  • An abstract class can also contains completed/normal methods along with abstract methods.
  • An abstract class can be used only by inheriting it to derived classes.
  • An abstract base class enforces to its derived class to must implement/complete its abstract method definition. If a derived class do not complete/implement abstract methods that class also must be declared as “abstract class” through used “abstract” keyword, the “abstract” keyword keeps continue inheriting to class’s hierarchy until the complete method implementation is achieved.
  • The abstract methods are “virtual methods” by default so explicitly “virtual” keyword cannot be specified with abstract methods.
  • Static methods cannot be specified/declared as “abstract”, or “abstract” modifier cannot be used with static methods, it only used with instance/non-static methods.
  • In C# properties and indexers can also be “abstract”.

General Syntax:

abstract class ClassName
        access-modifier abstract return-type MethodName (parameterList);

abstract It is a keyword.
class ClassName Here class is a keywords and ClassName can be any valid name of a class.
{ } Body of a class.
access-modifier Can be public.
return-type MethodName (parameterList); return-type can be void or any other type, MethodName can be any valid name of a method and (parameterList) can be optional local variables.


using System;
namespace csharpBasic
    // An abstract class declaration.
    abstract class MarkSheet
        // Fields declared with protected access.
        protected double AspMarks;
        protected double CSharpMarks;

        // Parametrized constructor which sets marks for each subject.
        public MarkSheet(double aspMarks, double cSharpMarks)
            this.AspMarks = aspMarks;
            this.CSharpMarks = cSharpMarks;

        // Abstract double return type method declaration.
        public abstract double GetObtainMarks();

        // Double return type method declaration which returns obtain marks of subjects.
        public double GetPercentage(double obtainMarks, double totalMarks)
            return (obtainMarks / totalMarks) * 100.0;

    // BcsMarkSheet class inherits an abstract MarkSheet class.
    class BcsMarkSheet : MarkSheet
        double PhpMarks;

        // Parametrized constructor which sets marks for each subject in derived class.
        public BcsMarkSheet(double aspMarks, double cSharpMarks, int phpMarks)
            : base(aspMarks, cSharpMarks)
            this.PhpMarks = phpMarks;

        // Double return type methods which returns each subject marks.
        public double GetAspMarks()
            return this.AspMarks;

        public double GetCsharpMarks()
            return this.CSharpMarks;

        public double GetPhpMarks()
            return this.PhpMarks;

        /*Notice the following method, 
          this is an abstract method in MarkSheet (base) class, 
          but this derived class is overriding/implementing/completing this method.*/
        public override double GetObtainMarks()
            return this.AspMarks + this.CSharpMarks + this.PhpMarks;

    class Program
        static void Main(string[] args)
            // Derived class object is being created.
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet(85, 70, 60);
            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1:");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetAspMarks());
            Console.WriteLine("Subject 2: Csharp");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetCsharpMarks());
            Console.WriteLine("Subject 3: Php");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetPhpMarks());
            Console.WriteLine("Obtain marks: {0}", bcsMarkSheet.GetObtainMarks());
            Console.WriteLine("Percentage: {0}", bcsMarkSheet.GetPercentage(bcsMarkSheet.GetObtainMarks(), 300));
    /*  The Output will be:
        Bcs Mark sheet information:
        Subject 1:
        Marks: 85
        Subject 2: Csharp
        Marks: 70
        Subject 3: Php
        Marks: 60
        Obtain marks: 215
        Percentage: 71.6666666666667

Next Tutorial →

← Previous Tutorial

Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .