By 22/07/2015 5 Comments

C# Multilevel Inheritance

  • C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.
  • If the constructors are defined by all classes in multilevel inheritance the “base” keyword refers to the most nearest constructor in the base/parent class for example there are three classes class A, B and C, we inherit A to B and B to C, the “base” keyword in class C will always refer to the constructor of class B and “base” of class B refers to constructor in class A.
  • If a constructor in base/parent class are parametrized constructor, then all derived/child classes must have those parameters.

General Syntax:

class A
{ }

class B : A
{ }

class C : B
{ }

class D : C
{ }

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // fields are 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;
        }

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

    // A class BcsMarkSheet which is derived from MarkSheet class.
    class BcsMarkSheet : MarkSheet
    {
        protected double PhpMarks;

        // Parametrized constructor which sets marks for each subject in derived class.
        public BcsMarkSheet(double aspMarks, double cSharpMarks, double 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;
        }

        public double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks + this.PhpMarks;
        }
    }

    // A class SoftwareEngMarksheet which is derived from BcsMarkSheet class.
    class SoftwareEngMarksheet : BcsMarkSheet
    {
        double JavaMarks;

        // Parametrized constructor which sets marks for each subject.
        public SoftwareEngMarksheet(double aspMarks, double cSharpMarks, double phpMarks, double javaMarks)
            : base(aspMarks, cSharpMarks, phpMarks)
        {
            this.JavaMarks = javaMarks;
        }

        public double GetJavaMarks()
        {
            return this.JavaMarks;
        }

        new public double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks + this.PhpMarks + this.JavaMarks;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // An object creation of SoftwareEngMarksheet class.
            SoftwareEngMarksheet softwareEngMarksheet = new SoftwareEngMarksheet(85, 70, 60, 55);

            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1: Asp.net");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetAspMarks());
            Console.WriteLine("Subject 2: Csharp");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetCsharpMarks());
            Console.WriteLine("Subject 3: Php");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetPhpMarks());
            Console.WriteLine("Subject 4: Java");
            Console.WriteLine("Marks: {0}", softwareEngMarksheet.GetJavaMarks());
            Console.WriteLine("Obtain marks: {0}", softwareEngMarksheet.GetObtainMarks());
            Console.WriteLine("Percentage: {0}", softwareEngMarksheet.GetPercentage(softwareEngMarksheet.GetObtainMarks(), 400));
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Bcs Mark sheet information:
        Subject 1: Asp.net
        Marks: 85
        Subject 2: Csharp
        Marks: 70
        Subject 3: Php
        Marks: 60
        Subject 4: Java
        Marks: 55
        Obtain marks: 270
        Percentage: 67.5
       
       */
    }
}






← 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 .