-1

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.

Vikram Singh
  • 435
  • 2
  • 10
  • 3
    Remove the interface implementation and you'll see that `base()` still works. That's because it belongs to `Object`, not to the interface. – 41686d6564 stands w. Palestine Sep 08 '20 at 19:03
  • 3
    Except for `object`, all classes in .NET derive from exactly one class. If a class declares no base class, it derives from `object` directly. Your `IEmployee` isn't a class and doesn't get involved in this – Flydog57 Sep 08 '20 at 19:03
  • 1
    I think this question starts from the incorrect assumption that a class implementing an interface is a derived class. It's not. It's a class that implements an interface – Camilo Terevinto Sep 08 '20 at 19:07
  • It _is_ a derived class though; it's derived from the `System.Object` class, and that's exactly why `: base()` works, @Camilo (I get what you're trying to say but this might confuse the OP). – 41686d6564 stands w. Palestine Sep 08 '20 at 19:15
  • @41686d6564 I disagree. The OP is already confused, and saying that it is a derived class because it inherits from Object is only going to make things worse, that's quite an extreme PoV – Camilo Terevinto Sep 08 '20 at 19:26
  • @CamiloTerevinto .Hi Camilo, no, I am not confused. I understand completely now, that the `Manager` class is inheriting implicitly from the `Object` class. The `IEmployee` is an interface and not a class - so the `Manager` class defaults to the `base` of `Object`, and not `IEmployee`. Thanks! – Vikram Singh Sep 08 '20 at 23:07

1 Answers1

1

Every class in C# derives from the Object base class.

The fact that C# uses the same keyword for class inheritance and interface implementation, the : symbol, has probably confused you.

When you call : base(), you aren't calling the interface's constructor. It does not have one. You are calling the constructor of System.object, that is a simple constructor with no arguments.

Try removing the interface and see that it still works.

dimlucas
  • 5,040
  • 7
  • 37
  • 54