Caling Base Class method from Derived Class in C#
This topic contains and tries to cover following subjects:
- Explanation of Base keyword in C#
- Syntax of Base keyword in C#
- Example of Base keyword in C#
Articles provides solution to following questions and issues:
- How to use base keyword in C#
- How to call a method of base Class from the derived Class
- Why would a programmer need to call a base Class method from derived Class in practice
Explanation of Base keyword in C#
Base keyword in C# enables programmers to call a method of Base Class from the derived Class.
Syntax of Base keyword in C#
Following syntax is used to call a method of base Class from the derived Class:
namespace NS_hardwareManagerApp
{
//declare a base Class...///
class CL_aBaseClass
{
public void FN_Info()
{
}
}
//create a derived Class and inherit base Class...///
class CL_aDerivedClass : CL_aBaseClass
{
public void FN_Info()
{
//call base Class method in derived Class...///
base.FN_Info();
}
}
class CL_x
{
static void Main(string [] args)
{
Console.ReadLine();
}
}
}
C
Example of Base keyword in C#
Following example indicates how to call a base Class method from derived Class. Example is a console application which is created and test with Visual Studio 2008.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NS_HRapplication
{
//declare a base Class...///
class CL_employees
{
public void FN_displaySalary()
{
Console.WriteLine("Salary is 1000 euro...");
}
}
//create a derived Class and inherit base Class...///
class CL_tehnicians : CL_employees
{
public void FN_displaySalary()
{
//we assume that managers are getting paid 1000 euro extra
than base employee salary...///
base.FN_displaySalary();
}
}
class CL_x
{
static void Main(string [] args)
{
//create a technician instance-object
.../// CL_tehnicians o_aTechnician = new CL_tehnicians();
//display salary...///
o_aTechnician.FN_displaySalary();
Console.ReadLine();
}
}
}
Application output: