Inheritance : Some Concepts


I am going to explain some specific concepts of inheritance which is hot topic of most .net interviews. First of all lets define Inheritance. From Wiki we have the following definition:
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, or parent classes. The resulting classes are known as derived classes, subclasses, or child classes. The relationships of classes through inheritance gives rise to a hierarchy. In prototype-based programming, objects can be defined directly from other objects without the need to define any classes, in which case this feature is called differential inheritance. The inheritance concept was invented in 1968 for Simula.
In .Net Classes can inherit from another class. This is accomplished by putting a colon after the class name when declaring the class, and naming the class to inherit from—the base class—after the colon, as follows:
public class A
{
    public A() { }
}

public class B : A
{
    public B() { }
}

Lets come to point, In most of the interview you will be asked : "Suppose you have a Class Parent which have virtual method TestMethod and another Class child which inherits parent class and overrides TestMethod, now we are creating object of parent class through child class and calling the TestMethod. Which method will be called, Parent or Child? If we remove virtual key word then what will happen? if there is no overriding then which method will me called?
Lets take the below example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObjectTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Parent ch = new Child();
            ch.TestMethod();
            ch.HideMethod();
            Console.WriteLine(ch.abc.ToString());
            Console.ReadLine();
        }
    }
    class Parent
    {
        public  int abc = 10;
        public Parent()
        {
            Console.WriteLine("It is Parent  Constructor");
        }
        public virtual void TestMethod()
        {
            Console.WriteLine("It is Parent  Method");
        }
        public void HideMethod()
        {
             Console.WriteLine("It is Parent Hide  Method");
        }

    }
    class Child : Parent
    {
        public new int abc = 20;
        public Child()
        {
            Console.WriteLine("It is Child  Constructor");
        }
        public override void TestMethod()
        {
            Console.WriteLine("It is Child  Method");
        }
        public new void HideMethod()
        {
            Console.WriteLine("It is Child Hide  Method");
        }
    }
}

If we run the above code we will get the below result:

Comments