Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Inheritance with virtual function
Inheritance with virtual function
Inheritance with virtual function


by Arun Singh on 6/6/2012 4:38:15 PM

Views: 1506       Comments: 0

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.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 6697
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.