Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Inheritance Interview Point of View

0.00/5 (No votes)
12 Sep 2017 1  
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.

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)
        {
            // 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.

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   //: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.

 

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   //: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");
        }
    }

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here