Following is the code:
namespace ClassLibrary1
{
public class Manager : IEmployee
{
private int _empId;
private string _empName;
private string _location;
public int EmpId
{
get
{
return _empId;
}
set
{
_empId = value;
}
}
public string EmpName
{
get
{
return _empName;
}
set
{
_empName = value;
}
}
public string Location
{
get
{
return _location;
}
set
{
_location = value;
}
}
public Manager(int empId, string empName, string Location)
: base()
{
this._empId = empId;
this._empName = empName;
this._location = Location;
}
public string GetHealthInsuranceAmount()
{
return "Additional Health Insurance Premium Amount is: 1000";
}
}
}
Here, the class Manager
implements the interface IEmployee
. And the interface should have no constructor.
So, how is it possible that the Manager constructor
is able to call the IEmployee constructor
?
Following is the IEmployee interface
:
namespace ClassLibrary1
{
public interface IEmployee
{
string GetHealthInsuranceAmount();
int EmpId
{
get; set;
}
string EmpName { get; set; }
string Location
{
get; set;
}
}
}
Here is the calling Program:
using ClassLibrary1;
using System;
namespace InheritanceExample
{
class Program
{
static void Main(string[] args)
{
IEmployee emp;
emp = new Manager(1001, "Vikram", "USA");
Console.WriteLine($"Managers EmpId: {emp.EmpId}. Name: {emp.EmpName}. Location: {emp.Location}");
Console.WriteLine($"Manager's health insurance: {emp.GetHealthInsuranceAmount()}");
//emp = new SalesMan(1002,"Sukhmeet","Austrailia");
//Console.WriteLine($"SalesMan EmpId: {emp.EmpId}. Name: {emp.EmpName}. Location: {emp.Location}");
//Console.WriteLine($"SalesMan's health insurance: {emp.GetHealthInsuranceAmount()}");
}
}
}
As shown above, following is the way, the Manager class constructor
, is calling the IEmployee interface constructor
:
public Manager(int empId, string empName, string Location)
: base()
{
this._empId = empId;
this._empName = empName;
this._location = Location;
}
Please see: I am using C# language version 7.3 (.Net Framework 4.8) - but that shouldn't matter.