By 22/07/2015 2 Comments

Method Overriding In C#

  • In inheritance when derived classes re-declare or re-implement the same method of its base class that process is called method overriding.
  • For override a method a derived class must implements that method which matches the name of the method, parameter-list, return type or signature in its base class.
  • In C# the methods which may be overridden in derived classes must be marked as keyword “virtual” in base classes. The “virtual” methods can be re-declared or re-defined in one or more derived classes, each derived class defines its own version of a “virtual” method.
  • When derived classes override a “virtual” methods that methods must be marked with keyword “override”.
  • In C# overridden methods are called at runtime not compile time that conceptual process or mechanism is called “dynamic method dispatch”. Through “dynamic method dispatch” C# implements one of the object oriented feature/principal “polymorphism” but runtime polymorphism or Overridden methods implement “polymorphism” to C#.
  • Method overriding allows to derived classes for add extra, new features, operations and its own updated implementation in exists “virtual” methods of its base classes.
  • The C# implements “one interface, multiple methods” through using method overriding.
  • In C# a base class reference variable can refers to its derived class object, when we assigns a derived class object to its base class reference variable that the base class reference calls to overridden methods of its derived classes not its own “virtual” methods, if there are no overridden methods declared in derived classes it executes its own (base) “virtual” method. Since it is not necessary to override “virtual” methods in derived classes.
  • In method overriding the C# compiler move bottom to top to find override methods if it finds executes it otherwise base “virtual” methods will be executed.
  • Properties and indexers can also be declared as “virtual” and “override”.
  • Abstract and static methods cannot be declared as “virtual” or “override”.

Example – 1:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // Fields declaration 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 is declared with virtual keyword.
        public virtual double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks;
        }

        // 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 MarkSheet
    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.AspMarks = aspMarks;
            this.CSharpMarks = 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 has been declared as a “virtual” method in base class.
        public override double GetObtainMarks()
        {
            return this.AspMarks + this.CSharpMarks + this.PhpMarks;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Object of derived class.
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet(85, 70, 60);

            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1: Asp.net");
            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));
            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
        Obtain marks: 215
        Percentage: 71.6666666666667
        
       */
    }
}


Example – 2, assign a derived class object to base class reference:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // Following void type methods are declared as virtual methods.
        public virtual void DisplayMessage1()
        {
            Console.WriteLine("Base class virtual method DisplayMessage1()...");
        }

        public virtual void DisplayMessage2()
        {
            Console.WriteLine("Base class virtual method DisplayMessage2()...");
        }
    }

    // BcsMarkSheet inherits MarkSheet.
    class BcsMarkSheet : MarkSheet
    {
        /* Virtual method of base class which is overridden using a 
           keyword “override” by derived class. */
        public override void DisplayMessage1()
        {
            Console.WriteLine("Derived class override method DisplayMessage1()...");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            /* Notice at following statement where 
               derived class object is being assigned into base class reference.*/
            MarkSheet markSheet = new BcsMarkSheet();

            // Virtual methods are called by base class reference.
            markSheet.DisplayMessage1();
            markSheet.DisplayMessage2();
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Derived class override method DisplayMessage1()...
        Base class virtual method DisplayMessage2()...
        
       */
    }
}





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