Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / C#
Tip/Trick

C# Inheritance Interview Point of View

Rate me:
Please Sign up or sign in to vote.
3.08/5 (13 votes)
12 Sep 2017CPOL2 min read 32.6K   5   4
C# Inheritance Interview Point of View

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.

C#
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.

Image 1

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.

C#
static void Main(string[] args)
        {
            // Nothing special - It will call parent constructor and print method

            Console.WriteLine(" ---- Parent -----");
            Parent p = new Parent();
            p.Print();

            // Now point to note is Parent constructor call first and child 
            Console.WriteLine(" ---- Child -----");
            Child C = new Child();
            C.Print();

            // Parent method will be called not child method
            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.

Image 2

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.

C#
public class Parent   //:SuperParent
    {
        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.

Image 3

 

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.

C#
public class Parent   //:SuperParent
    {
         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");
        }
    }

Image 4

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerDifferent answer Pin
Member 1102664423-Jun-17 12:32
Member 1102664423-Jun-17 12:32 
GeneralRe: Different answer Pin
Rajasakthimaridasan12-Sep-17 21:02
Rajasakthimaridasan12-Sep-17 21:02 
GeneralMy vote of 5 Pin
Member 819701315-Jan-17 20:22
Member 819701315-Jan-17 20:22 
PraiseNice demo Pin
asiwel30-Nov-16 6:49
professionalasiwel30-Nov-16 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.