By 22/07/2015 9 Comments

C# Base Class Reference Derived Class Object

  • In C#, inheritance derived class object reference can be assigned to the base class reference variable.
  • Remember, only reference of a derived class is assigned to the base class reference variable not derived class’s object.
  • Base class reference variable cannot access its derived class members, it can access only its own members because base class has no knowledge about its derived/child class.

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // Fields declaration with protected access.
        protected double AspMarks;
        protected double JsMarks;

        // Constructor which creates a Marksheet by assigning different values to its each subjects.
        public MarkSheet(double aspMarks, double jsMarks)
        {
            this.AspMarks = aspMarks;
            this.JsMarks = jsMarks;
        }

        // Return types methods returns each subject marks.
        public double GetAspMarks()
        {
            return this.AspMarks;
        }
        public double GetJsMarks()
        {
            return this.JsMarks;
        }
    }

    // BcsMarkSheet class inherits MarkSheet
    class BcsMarkSheet : MarkSheet
    {
        double PhpMarks;

        // Derived class constructor which constructs derived class marksheet.
        public BcsMarkSheet(double phpMarks)
            : base(85, 50)
        {
            this.PhpMarks = phpMarks;
        }

        public double GetPhpMarks()
        {
            return this.PhpMarks;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Objects creation of two classes (derived BcsMarkSheet and base class marksheet).
            MarkSheet markSheet = new MarkSheet(0, 0);
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet(65);

            // Derived object reference is being assigned to base class reference variable.
            markSheet = bcsMarkSheet;
      
            // Print statements which print both marksheets information.
            Console.WriteLine("Marksheet base class information:");
            Console.WriteLine("Asp.net marks: {0}", markSheet.GetAspMarks());
            Console.WriteLine("Javascript marks: {0}", markSheet.GetJsMarks());
            Console.WriteLine("Bcs marksheet derived class information:");
            Console.WriteLine("Php marks: {0}", bcsMarkSheet.GetPhpMarks());
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Marksheet base class information:
        Asp.net marks: 85
        Javascript marks: 50
        Bcs marksheet derived class information:
        Php marks: 65
         
       */
    }
}






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 .