Simple inheritance using C#

by Jagadish Pulakhandam on 2/13/2012 12:35:05 PM
Rated 0 from 0 votes
Brief: Demonstrates on inheriting members from parent class into child class
Posted to: Inheritance in .NET Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Inheritance - A class can inherit members from another class (without redefining all those members again).  I can have a parent class called "Person" which contains Two properties "FirstName" and "LastName".  I can have a child class named "Employee" which inherits from "Person" class.

When a class is inherited from parent class, all the members available in parent class are available to child class (logically).  Child class can have its own members (apart from the ones already inherited).  The child class members will not be carried to parent.  The hierarchy is always one way down (i.e., Parent-to-child) and not the other way around.

When an instance of child class is created, .NET run time creates memory allocation for every member in child class along with every member in parent class.  As the parent members are logically available in child class, we can directly instantiate child class and access both child and parent members using the same object.

The parent/child terminology can be exchanged as base/derived or super/sub (depending on the language used).

The following example shows the following:

  • How to define a Parent class (base class or super class)
  • How to define a Child class inheriting members from parent (derived from base or sub class from super class)
  • How to instantiate a derived class and access members in base class

Example:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace SimpleInheritance
07.{
08.    public class Person
09.    {
10.        //classic way of defining a property
11.        private string _FirstName;
12.        public string FirstName
13.        {
14.            get
15.            {
16.                return _FirstName;
17.            }
18.            set
19.            {
20.                _FirstName = value;
21.            }
22.        }
23. 
24. 
25.        //simple/compact way of defining a property with get/set operations
26.        //internally/implicitly maintains a private variable
27.        public string LastName { get; set; }
28.    }
29.}
01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace SimpleInheritance
07.{
08.    public class Employee : Person //"Employee" class, now contains all properties of "Person" class
09.    {
10.        public int EmployeeID { get; set; }
11. 
12.        //the "FirstName" and "LastName" properties in "Person" class
13.        //are automatically carried into this class.  We need not redefine them.
14.    }
15.}
01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09. 
10.namespace SimpleInheritance
11.{
12.    public partial class Form1 : Form
13.    {
14.        public Form1()
15.        {
16.            InitializeComponent();
17.        }
18. 
19.        private void button1_Click(object sender, EventArgs e)
20.        {
21.            Person p = new Person();
22.            p.LastName = "Jag";
23.            p.FirstName = "Chat";
24.             
25.            //this would not work, as "EmployeeID" property is not defined in "Person" class
26.            //p.EmployeeID = 1001;
27. 
28.            Employee oEmp = new Employee();
29.            oEmp.EmployeeID = 1001; //works as "EmployeeID" property is defined in "Employee" class
30.            oEmp.FirstName = "Win"; //works as "FirstName" is defined in "Person" class and is brought into "Employee" class through inheritance.
31.            oEmp.LastName = "Others"; //works as "LastName" is defined in "Person" class and is brought into "Employee" class through inheritance.
32. 
33.            //FirstName and LastName properties work through "Employee" object, as if they are really in "Employee" class
34.            MessageBox.Show(string.Format("Name = '{0} {1}'", oEmp.FirstName, oEmp.LastName));
35.        }
36.    }
37.}



Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following