By 22/07/2015 21 Comments

Inheritance In C#

  • C# Inheritance is one of the basic principal of object oriented programming.
  • Inheritance describes the hierarchically classifications between the objects.
  • In general terms inheritance defines the “IS A” relationship between objects such as a Mark sheet IS An university mark sheet, a saving account IS A type of bank account, dog IS AN animal IS A mammal and an employee IS A person etc., so we can easily find inheritance situation by recognized a “IS A” relationship between objects.
  • We can implement inheritance principal in our C# programs by defining multiple classes.
  • In C# programming, inheritance can be implemented by inherit one class into other classes, the class which is being inherited is called base, father, parent or super class and the class which is inheriting is called a child, sub or derived class.
  • When a base class is being inherited into its derived class all members (methods, indexers, properties and instance variables/fields) of base class automatically inherited into its derived class except those members which has their “private” access because private members do not inherit, now all base class public members can also be accessed by derived class object, derived class is free and can also be defined its own members (methods, properties, indexers and instance variables etc.) and can also override/modified any member of a base class in it. The base class does not have knowledge about any it’s derived class and it cannot access its derived class members but base class is free and can access its own members.

General Syntax:

class DerivedClass : BaseClass
Body of a class.

class DerivedClass Name of a class which is inheriting also called derived class.
: BaseClass Name of a class which is being inherited also called base class. The colon (:) sign is indicating that one class (base class) is being inherited into other (derived class).
{ Body of a class. } Body of a class, any class member can be declared/defined here.


using System;
namespace csharpBasic
    // A base class declaration.
    class MarkSheet
        public string AspSubject;
        public string CSharpSubject;
        public double AspSubjectMarks;
        public double CSharpSubjectMarks;

        // Following double type methods are declared.
        public virtual double GetObtainMarks()
            return this.AspSubjectMarks + this.CSharpSubjectMarks;

        public double GetPercentage(double obtainMarks, double totalMarks)
            return (obtainMarks / totalMarks) * 100.0;

    // A derived (BcsMarkSheet) class declaration which is inherited from base class (MarkSheet).
    class BcsMarkSheet : MarkSheet
        public string PhpSubject;
        public double PhpSubjectMarks;

        // A double type method declaration.
        public override double GetObtainMarks()
            return this.PhpSubjectMarks + this.AspSubjectMarks + this.CSharpSubjectMarks;

    class Program
        static void Main(string[] args)
            // Object creation of BcsMarkSheet.
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet();

            // Base class fields are accessed through its derived class reference.
            bcsMarkSheet.AspSubject = "";
            bcsMarkSheet.CSharpSubject = "";
            bcsMarkSheet.PhpSubject = "Php";
            bcsMarkSheet.AspSubjectMarks = 85;
            bcsMarkSheet.CSharpSubjectMarks = 90;
            bcsMarkSheet.PhpSubjectMarks = 56;
            // Different types of information is being printed.
            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1: {0}", bcsMarkSheet.AspSubject);
            Console.WriteLine("Marks: {0}", bcsMarkSheet.AspSubjectMarks);

            Console.WriteLine("Subject 2: {0}", bcsMarkSheet.CSharpSubject);
            Console.WriteLine("Marks: {0}", bcsMarkSheet.CSharpSubjectMarks);

            Console.WriteLine("Subject 3: {0}", bcsMarkSheet.PhpSubject);
            Console.WriteLine("Marks: {0}", bcsMarkSheet.PhpSubjectMarks);

            Console.WriteLine("Obtain marks: {0}", bcsMarkSheet.GetObtainMarks());
            Console.WriteLine("Perentage: {0}", bcsMarkSheet.GetPercentage(bcsMarkSheet.GetObtainMarks(), 300) + "%");
        The Output will be:
        Bcs Mark sheet information:
        Subject 1:
        Marks: 85
        Subject 2: 
        Marks: 90
        Subject 3: Php
        Marks: 56
        Obtain marks: 231
        Perentage: 77 %


  • In C# only multilevel inheritance is supported which means we can inherits only one base class for derived class at a time and cannot inherits more than one/multiple base classes into a single derived class at a time.
  • Same base class can inherits the several numbers of derived classes.
  • In inheritance any derived classes can become the base classes of another classes for instance class A can inherits into class B, class B can inherits into class C, class C can inherits into class D and so on that chain is called multilevel inheritance or hierarchy.
  • There are two main advantages of using inheritance those are: if we works on multiple classes and we find some common members in these classes than we create a base class for those classes, second inheritance prevents duplication and re-writing of class members.

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 .