Click here to Skip to main content
13,141,747 members (66,273 online)
Click here to Skip to main content
Add your own
alternative version

Stats

7.1K views
3 bookmarked
Posted 25 Nov 2016

C# Inheritance Interview Point of View

, 12 Sep 2017
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajasakthimaridasan
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
AnswerDifferent answer Pin
Member 1102664423-Jun-17 12:32
memberMember 1102664423-Jun-17 12:32 
GeneralRe: Different answer Pin
Rajasakthimaridasan12-Sep-17 21:02
memberRajasakthimaridasan12-Sep-17 21:02 
GeneralMy vote of 5 Pin
Member 819701315-Jan-17 20:22
memberMember 819701315-Jan-17 20:22 
PraiseNice demo Pin
asiwel30-Nov-16 6:49
memberasiwel30-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 13 Sep 2017
Article Copyright 2016 by Rajasakthimaridasan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid