Monday, 20 February 2017

Run time polymorphism. Necessity for Virtual,Override and New



                       Virtual,Override and NEW


1. What is the meaning of 'DerivedClass_B.X()' hides inherited member 'BaseClass_A.X()'. Use the new keyword if hiding was intended?

Ans: Visual studio gives this warning as precaution when in future if you
we have same method in both the classes.C# compilers just warns because it thinks that
We have put our effort to write a method in derived class and if we really want to hide to it keep hidden use new ..Compiler will stop worrying about it.

2. Why does base class method is called when below code is executed?
             
BaseClass_A ObjBaseX = new DerivedClass_B();
ObjBaseX.X();

Ans:

Lets start with the basics... When an object is created it just gets some space.

If Derived class object references to derived class object like

BaseClass_A ObjBaseX = new DerivedClass_B();
           
and if a method is called like

ObjBaseX.X();

Method written for Base class will be called.

Methods are not instance specific. Object's won't have copies of methods of their own'
           
 As methods won't contain any data,they are just set of lines of code.


 * So now it's clear why C# compiler gives warning. It wants to know if method written in derived class is intentional or accidental.
 
  For that we have we need to mention virtual in the base class method
  If derived class method is to be given top priority keep Override.
  If derived class method has nothing to do with base class Keep New.
 





Example Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritanceAndPolymorphism
{
    class Program
    {
        static void Main(string[] args)
        {

            //// Base class methods.
            BaseClass_A objBase = new BaseClass_A();
            objBase.X();
            objBase.Y();
            objBase.Z();

            BaseClass_A ObjBaseX = new DerivedClass_B();
            ObjBaseX.X();
            ObjBaseX.Y();
            ObjBaseX.Z();

            Console.ReadLine();
        }

        interface IStudent
        {
            BaseClass_A GetStudent();
            int GetInt();
        }
    }

       


    class BaseClass_A
    {
        public void X()
        {
            Console.WriteLine("BaseClass_A Method_X");
        }
        public virtual void Y()
        {
            Console.WriteLine("BaseClass_A Method_Y");
        }
        public virtual void Z()
        {
            Console.WriteLine("BaseClass_A Method_Z");
        }
    }
    class DerivedClass_B : BaseClass_A
    {

        public void X()
        {
            Console.WriteLine("");
        }
        public override void Y()
        {
            Console.WriteLine("DerivedClass_B Method_Y");
        }
        public new void Z()
        {
            Console.WriteLine("DerivedClass_B Method_Z");
        }
    }
}


No comments:

Post a Comment