Introduction
We know that inheritance is to provide parent child relationship between classes. The main purpose of this article is to explain inheritance from an interview point of view . I will be explaining it with a sample.
Simple Inheritance
In the below code, I have a class called Parent
and that is inherited by child class called Child.
Point to note here is both classes have a method with the same name called print.
public class Parent
{
public Parent()
{
Console.WriteLine(" I am a Parent Constructor");
}
public void Print()
{
Console.WriteLine(" I am a Parent");
}
}
public class Child : Parent
{
public Child()
{
Console.WriteLine(" I am a Child Constructor");
}
public void Print()
{
Console.WriteLine(" I am a Child");
}
}
If I compile this code, VS shows the below warning and not an error. It concludes that both classes can have the same method.
If see the warning, it says Child.Print()
hides parent method and asks to use new
keyword. So, using new
for hiding is not mandatory.
Now, come to instance creation and method calling.
static void Main(string[] args)
{
Console.WriteLine(" ---- Parent -----");
Parent p = new Parent();
p.Print();
Console.WriteLine(" ---- Child -----");
Child C = new Child();
C.Print();
Console.WriteLine(" ---- Parent via child -----");
Parent PC = new Child();
PC.Print();
Console.ReadKey();
}
If you see the below output, nothing is special in parent instance, it is just calling its constructor and method.
If you see the second part (child), then notice that parent constructor is called first, then its child.
The third instance is created for child with type of parent (i.e., Parent PC = new Child()
). Above it, call parent constructor, it's also calling child constructor. The point to note here is its calling parent method.
Point to note here is it always call the method of type of the instance. Here, instance type is parent.
Use of Virtual and Override
In the third type, if you want to call child method instead of parent method, then virtual
and override
come into the picture.
public class Parent
{
public Parent()
{
Console.WriteLine(" I am a Parent Constructor");
}
public virtual void Print()
{
Console.WriteLine(" I am a Parent");
}
}
public class Child : Parent
{
public Child()
{
Console.WriteLine(" I am a Child Constructor");
}
public override void Print()
{
Console.WriteLine(" I am a Child");
}
}
If you see the below output, the first two cases are same, only the third case is discussed. It overrides parent method.
Static Constructor
It will be executed only once, unlike the normal (or you can say the instance) constructor that is executed whenever the object is created.
And if you create an object for child, it calls child static
first and the drive d.
public class Parent
{
static Parent()
{
Console.WriteLine(" I am a Static Parent Constructor");
}
public Parent()
{
Console.WriteLine(" I am a Parent Constructor");
}
public void Print()
{
Console.WriteLine(" I am a Parent");
}
}
public class Child : Parent
{
static Child()
{
Console.WriteLine(" I am a Static Child Constructor");
}
public Child()
{
Console.WriteLine(" I am a Child Constructor");
}
public void Print()
{
Console.WriteLine(" I am a Child");
}
}
Points of Interest
- If you create child instance, parent constructor is called first the child constructor.
- Static constructor is called only once (first instance only) For child instance, child static constructor and the parent static.
- If you create child instance of parent type, the parent method is called. If you want to override, then use
Virtual
and override
.