Home > DeveloperSection > Articles > Inheritance with virtual function

Inheritance with virtual function


C# C# 
Ratings:
0 Comment(s)
 4035  View(s)
Rate this:

Inheritance with virtual function

Example of inheritance with virtual function:

For example create Employee class contains information about an Employee. Create another Type class that inherits the Employee class. Type class contains information employee is fulltime or part-time and also has a Display function that is overridden. This Type class inherits the Employee class.

Design the form and give the name frmEmpWithVirtual.

Inheritance with virtual function

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

Click on button ‘Submit’.

Inheritance with virtual function

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

Inheritance with virtual function

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

Inheritance with virtual function

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

Inheritance with virtual function

This message box display the Type class constructor called.

Inheritance with virtual function

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

Inheritance with virtual function

Inheritance with virtual function

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

Inheritance with virtual function

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


Don't want to miss updates? Please click the below button!

Follow MindStick