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
    API(s)
    Sencha-Touch
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# >> Introduction to inheritance
Introduction to inheritance
Introduction to inheritance


by Arun Singh on 6/6/2012 4:52:31 PM

Views: 962       Comments: 0

Introduction to inheritance

Inheritance is a mechanism in which property of base or parent class is automatically derived into the child or derived class. It is a concept of OOPs and advantage is reusability.

When we define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and destructors. The derived class can thereby reuse the code in the base class without having to re-implement it. In the derived class, you can add more members. In this manner, the derived class extends the functionality of the base class.

Simple example of inheritance is-

Here I will create Employee class that is a base class contains information about an Employee and another class is Type class that is child class contain information about employee is fulltime or part-time. This Type class inherits the Employee class.

Design the form and give the name frmEmpDetails.

Introduction to inheritance

Write down the 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 frmEmpDetails : Form

    {

        public frmEmpDetails()

        {

            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 void Display()   //creating 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 = "";

 

                MessageBox.Show("Call Type class constructor.....");

            }

            public void Show()//creating function called Show

            {

                Display();//call the function of Employee class Displsy

                MessageBox.Show("Type class Show funtion...\nemployee type..." + TypeName);

            }

 

        }

 

        private void buttonSubmit_Click(object sender, EventArgs e)

        {

            Type t1 = new Type();//define the object of derived class

//here we can use the base class variables by creating object of Type class

            t1.EmpID = Int32.Parse(txtEmpId.Text); 

            t1.Name= txtName.Text;

            t1.FatherName = txtEmpFatherName.Text;

            t1.Designation = txtDesignation.Text;

            t1.TypeName = txtEmaployeetype.Text;

 

          

            t1.Show();//call the derived class method

 

        }

 

    }

}

 

 

Run the form and Fill the required fields.

Introduction to inheritance

Click on Submit Button.

Introduction to inheritance

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

 

Introduction to inheritance

This message box displays the Type class constructor called.

Introduction to inheritance

Introduction to inheritance

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

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: 2748
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.