Inheritance and confusion between compile time vs run time
Inheritance and confusion between compile time vs run time

Inheritance allows a derived class to have the same characteristics of a base class that it is deriving from. The base class itself in this case cannot be a private or protected. We can have many derived classes from the same base class. For example, we can have a base class called “A” and then we can create classes like “B”, “C” etc derived from the same “A” class. In C# “:” is used to indicate that it’s an inheritance.

Table of contents

Things to remember

There are couple of things to keep in mind while dealing with inheritance. For instance, 
1) A derived class cannot have more than one base class. eg: Class B cannot have a base class A and C at the same time 
2) It is possible to assign derived class in an instance of a base class. eg: A a = new B();
3) It is not possible to assign a derived class in an instance of a derived class. eg: B b = new C(); will not compile.
4) If there is a parameterized constructor in the base class then we need to fulfil the requirement of the base class’s constructor while defining a constructor of the derived class. eg:

class A {
    private string _name;
    public A (string name){
        _name = name;
       }
}
 
class B : A {
    private string _name;
    public B (string name):base(name){
        _name = name;
        }
}

This is because, when we are creating an instance of a derived class, practically we are referring to a base class instance too..
5) If there is a virtual method in the base class then it is possible to override that method in the derived class.

class A {
    public virtual void SayHello(){
    Console.WriteLine(“Hello!!!”);
        }
}
 
class B : A {
    public override void SayHello(){
    WriteToAFile(“---Hello---”);
        }
}

In an overridden method it is possible to do something completely different than the base class, at the same time it is also possible to consume the same behaviour from the base class.

class B : A {
    public override void SayHello(){
    base.SayHello();
    WriteToAFile(“---Hello---”);
        }
}

Confusion?

Virtual methods are always dispatched based on the type of the object. Meaning, when we call a virtual method on some instance, the type of the object gets checked at runtime. And then the method from that type of object gets invoked.

class A {
    public virtual void ShowA()
        {
            Console.WriteLine("from : A");
        }
}
 
class B : A {
    public override void ShowA()
        {
            Console.WriteLine("from : B");
        }
}

Now, if we create an instance of type B and call the ShowA() method like below:

// left side is Compile-time and right side is Run-time
A a = new B();
a.ShowA(); // will show "from B"

This part is very tricky and important in terms of C#. In C# define-time or compile-time does not matter, run-time matters. Say what? Okay, let’s explain...

When we defined the “a” variable, compiler saw it as a variable of type “A”. Then when we are assigning that “a” variable to new instance of “B”, a memory location will be created to store a variable of type “B”. That’s why during run-time variable “a” becomes an instance of type B not A. Therefore, the method “ShowA()” from class “B” gets invoked.
 

February 26, 2016
Jahan Sarwar