---
title: "Inheritance with virtual function"  
description: "For example create Employee class contains information about an Employee. Create another Type class that inherits the Employee class. Type class conta"  
author: "Anonymous User"  
published: 2012-06-06  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/945/inheritance-with-virtual-function  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Inheritance with virtual function

##### Example of inheritance with virtual function:

For example create [Employee](https://www.mindstick.com/articles/178134/how-to-write-the-best-welcome-letter-for-new-employees) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) contains information about an Employee. Create another Type class that [inherits](https://www.mindstick.com/interview/85/what-is-a-subclass) the Employee class. Type class contains information employee is fulltime or part-time and also has a Display [function](https://www.mindstick.com/articles/43970/purchase-suitable-wedding-dresses-for-your-wedding-function) that is overridden. This Type class inherits the Employee class.

[Design](https://www.mindstick.com/articles/23215/the-secret-to-a-compelling-logo-design) the [form](https://yourviews.mindstick.com/view/87344/explained-the-benefits-of-short-form-videos) and give the [name](https://answers.mindstick.com/qa/38468/name-the-bollywood-actor-who-has-been-named-by-the-indian-olympic-association-as-the-country-s-goodwill-ambassador-representing-the-indian-contingent-at-the-2016-rio-olympics) frmEmpWithVirtual.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/8e1e15cf-4310-419c-a610-050573c8a933.png)

Write down following line of Code-

```
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace inheritance{    public partial class frmEmpWithVirtual : Form    {        public frmEmpWithVirtual()        {            InitializeComponent();        }        class Employee  // creating class employee        {            public int EmpID //define the property of variables EmpID            {                get;                set;             }            public string Name //define the property of variable Name            {                get;                set;            }            public string FatherName //define the property of variable FatherName            {                get;                set;            }            public string Designation  //define the property of variable Designetion            {                get;                set;            }             public Employee()  //constructor of Employee class to give their default value            {                EmpID = 0;                Name = "";                FatherName = "";                Designation = "";                 MessageBox.Show("Call Employee class constructor.....");            }            public virtual void Display()   //creating virtual function called Display            {                MessageBox.Show("Employee class Display function....\nempleyee id.." + EmpID + "\nemployee name.." + Name + "\nemployee father name..." + FatherName + "\nemployee desigination..." + Designation);             }         }        class Type : Employee  //child class Type inherrite the base class Employee        {            public string TypeName  //define the property of Type_Name            {                get;                set;            }            public Type() //constructor of Type class            {                TypeName = "Employee";                 MessageBox.Show("Call Type class constructor.....");            }            public override void Display()//Override the function called Display            {                MessageBox.Show("Employee class Display function....\nempleyee id.." + EmpID + "\nemployee name.." + Name + "\nemployee father name..." + FatherName + "\nemployee desigination..." + Designation +"\nEmployee Type.."+ TypeName);            }         }         private void buttonSubmit_Click(object sender, EventArgs e)        {            Employee t1 = new Employee();          //creating object of Employee class            t1.EmpID = Int32.Parse(txtEmpId.Text);            t1.Name = txtName.Text;            t1.FatherName = txtEmpFatherName.Text;            t1.Designation = txtDesignation.Text;            t1.Display();  //Employee class function Display called             t1 = new Type(); //initialize the Type class with object of Employee class            t1.EmpID = Int32.Parse(txtEmpId.Text);            t1.Name = txtName.Text;            t1.FatherName = txtEmpFatherName.Text;            t1.Designation = txtDesignation.Text;            t1.Display(); //type class function Display called             Type t2 = new Type(); //crating object of Type class            t2.EmpID = Int32.Parse(txtEmpId.Text);            t2.Name = txtName.Text;            t2.FatherName = txtEmpFatherName.Text;            t2.Designation = txtDesignation.Text;            t2.TypeName = txtEmaployeetype.Text;            t2.Display();  //type class function Display called        }    }} 
```

\

Execute program and fill all fields with appropriate information.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/c560a6ea-cc3c-4923-b774-7cb32f9ec053.png)

[Click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) on [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) ‘**Submit’**.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/00910b61-a3c5-42a6-9dd8-8015e77e671e.png)

There is message box display which is show that Employee class constructor called.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/237ff6cf-d1d6-4ea2-9922-f88919085a6c.png)

This [message](https://www.mindstick.com/interview/682/which-are-the-various-message-map-macros) box displays the information of Employee by calling Display function of Employee class.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/820babc4-3e64-4fc8-9ade-1b76d42e40a8.png)

Again Employee class constructor called by instantiation of Type class in Employee class object.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/d9311cb0-8006-4919-bbe8-e8df08ee7714.png)

This message box display the Type class constructor called.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/35ac85db-1bec-401b-9099-aa1d006cc3a9.png)

This message box displays the information of Employee by calling overridden Display function of Type class.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/519f73b3-fab2-401d-96b8-8b6a8b1997d2.png)

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/ccbda5ac-8009-48f9-b6ff-bcff82061c48.png)

Again Employee and Type class constructor called by creating new object of Type class.

![Inheritance with virtual function](https://www.mindstick.com/mindstickarticle/5de5db5d-7557-4c66-a5aa-1253f1973c89/images/a9541323-4787-4932-b077-d5df66f158ea.png)

This message box displays the information of Employee by calling overridden Display function of Type class.

\

---

Original Source: https://www.mindstick.com/articles/945/inheritance-with-virtual-function

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
