C# tutorial OOP-class inheritance

Class inheritance



In the object-oriented program, inheritance allows a class to use the members of another class. It encourages the reuse of exting components in software project in other software projects so that you can save time in software development. The class that allows other class to use its members is called base class and the class that uses or inherits other class' members is called derived class. Besides resuing the members of the base class, the derived class can customize those members to fullfil the requirements.

To create inheritance, use the following syntax:

class derived_classname:access_level baseclass1, baseclase2,...{ }

access_level can be public, protected, or private

For example, a general employee will have name, sex, salary information. A secretary will have the information above plus the typing speed. So It is possible to create an Employee class which contains the name, sex, salary information and a Secretary class that inherits the Employee class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

class Employee
{
protected string emName;
protected string emSex;
protected double emSalary;
public Employee()
{
emName = "";
emSex = "";
emSalary = 0.0;
}
public Employee(string emName, string emSex, double emSalary)
{
this.emName = emName;
this.emSex = emSex;
this.emSalary = emSalary;

}

protected void setName(String name)
{
emName = name;
}

protected void setSex(String sex)
{
emSex = sex;
}
protected void setName(double salary)
{
emSalary = salary;
}

public virtual void printEmpInfo()
{
Console.WriteLine("Name:{0},Sex:{1},Salary:{2}", emName, emSex, emSalary);
}
}
class Secretary : Employee
{
private int typing_speed;
public Secretary(int speed)
{
typing_speed = speed;

}

public Secretary()
: base()
{
typing_speed = 0;
}

public Secretary(string Name, string Sex, double Salary, int speed)
: base(Name, Sex, Salary)
{
typing_speed = speed;

}

public void setTypingSpeed(int speed)
{
typing_speed = speed;
}
public void printSecInfo()
{
Console.WriteLine("Name:{0},Sex:{1},Salary:{2}, Typing_speed:{3}", emName, emSex, emSalary, typing_speed);
}

}

class Program
{
static void Main(string[] args)
{
Secretary sec = new Secretary("Dara Chan", "M", 5000.0,100);
//sec.setTypingSpeed(100);
sec.printEmpInfo();
sec.printSecInfo();
Console.ReadLine();
}
}

}

The base keyword (the same as super keyword in Java) will be used in derived class to specify the constructor of the base class to be called when the constructor of the derived class is called. The constructor of the base class is called before the constructor of the derived class.

 

Using virtual and override keywords

The base class and derived class might contain methods that have the same name. In this scenario, the method of the derived class will override the method of the base class. You will use the virtual keyword with the method of the base class and override keyword with the method of the derived class.

public virtual void printInfo(){

...............................

}

public override void printInfo(){

................................

}

 



Comments

Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like