Understand Inheritance and Polymorphism with example

By | October 6, 2017

In this post, we will understand Inheritance and Polymorphism concept of OOP’S practically. In most of the interviews, interviewers ask these inheritance questions and try to confuse you but after reading this post you will easily understand and clear all your confusion.

When you derive a class from a base class, the derived class will inherit all members of the base class except constructors, though whether the derived class would be able to access those members would depend upon the accessibility of those members in the base class. C# gives us polymorphism through inheritance. Inheritance-based polymorphism allows us to define methods in a base class and override them with derived class implementations. Thus if you have a base class object that might be holding one of several derived class objects, polymorphism when properly used allows you to call a method that will work differently according to the type of derived class the object belongs to.

Consider the below example where we have created two class with constructor and same method name.

public class A
 {
 public A()
 {
 Console.WriteLine("A Constructor");
 }
 public void Display()
 {
 Console.WriteLine("A Class: Display");
 }
 }
 public class B : A
 {
 public B()
 {
 Console.WriteLine("B Constructor");
 }
 public void Display()
 {
 Console.WriteLine("B Class: Display");
 }
 }
 class Program
 {
 static void Main(string[] args)
 {
 A obj = new B();
 obj.Display();
 Console.ReadLine();
 }
 }

In above example, we have created an object of child class “B”  assign in reference of class “A”, then it gives the below output.

Output:-
A Constructor
B Constructor
A Class: Display

Now, we will take one more example by creating both the class objects and assign vise versa as below:

 public class A
 {
 public A()
 {
 Console.WriteLine("A Constructor");
 }
 public void Display()
 {
 Console.WriteLine("A Class: Display");
 }
 }
 public class B : A
 {
 public B()
 {
 Console.WriteLine("B Constructor");
 }
 public void Display()
 {
 Console.WriteLine("B Class: Display");
 }
 }
 class Program
 {
 static void Main(string[] args)
 {
 A obj1 = new A();
 B obj2 = new B();
 A obj3 = new B();
 obj1.Display();
 obj2.Display();
 obj3.Display();
 Console.ReadLine();
 }
 }

In the above example, we have created three objects. At run-time obj1 will call only base class constructor and obj2 & obj3 will call first base class constructor then their own or child class.

Output:-
A Constructor
A Constructor
B Constructor
A Constructor
B Constructor
A Class: Display
B Class: Display
A Class: Display

Now, we will create two class (A and B). Both class will have three method with same name like Display1, Display2 and Display3. In this example, we will use virtual, override and new keyword(hiding the warning message for same method name in inheritance).

 

 

I hope you will enjoy the Inheritance and Polymorphism practical questions with example. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Like it? Share it