First thing first. I hope my title is not misleading. I tried my best to phrase it.
Now, see the code below. Case 1 is pretty straight forward. Both cases work as expected. My question is why does compiler allow Case 2? Are there specific scenarios when this is desired. I cannot think of one.
interface IEmployee
{
void Register(string role);
}
abstract class Employee : IEmployee
{
public void Register(string role)
{
Console.WriteLine(role);
}
}
// Case 1
class Manager : Employee
{
}
// Case 2
class Developer : Employee, IEmployee
{
}
class Test
{
public void Test1()
{
IEmployee emp1 = new Manager();
emp1.Register("manager"); // output "manager"
IEmployee emp2 = new Developer();
emp2.Register("developer"); // output "developer"
}
}
A class can inherit from one superclass and can implement as many interfaces as it wishes. In response to Eric's comment... I had a discussion with another developer about whether or not interfaces "inherit", "implement", "require", or "bring along" interfaces with a declaration like:
References. If both interfaces have a method of exactly the same name and signature, the implementing class can implement both interface methods with a single concrete method. However, if the semantic contracts of the two interface method are contradicting, you've pretty much lost; you cannot implement both interfaces in a single class then.
} } The derived class doesn't directly implement the interface itself, it merely inherits the interface implementation from its base class (and then chooses to override it, but that's not a requirement, it could choose to keep the base method as is).
There is no inherent requirement which states that methods should be implemented via an interface and fields/properties should be inherited/defined in the class. This is not a good way to separate the logic, as you're essentially preventing any meaningful relation to exist between a class' fields/properties and its methods.
edit
as i expected the answer can be found in the c# specification
some taglines:
13.4.5 Interface implementation inheritance
Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface mappings it inherits from its base classes
13.4.6 Interface re-implementation
A class that inherits an interface implementation is permitted to re-implement the interface by including it in the base class list
read more to study all cases (digital copy can be found in Visual Studio folders)
public interface IEmployee
{
void Register(string role);
}
public abstract class Employee : IEmployee
{
public void Register(string role)
{
Console.WriteLine(role);
}
}
// Case 2
public class Developer : Employee, IEmployee
{
// this will not work without IEmployee in declaration!
void IEmployee.Register(string role)
{
Console.WriteLine("i'm developer!");
}
}
public class Program
{
public static void Main()
{
var dev = new Developer();
dev.Register("senior");
IEmployee e = dev;
e.Register("senior");
}
}
program print:
senior
i'm developer!
first value comes from Employee.Register
second value - from Developer.Register
if Developer
defined as
// Case 2
public class Developer : Employee
{
public void Register(string role)
{
Console.WriteLine("i'm developer!");
}
}
output of the same program is:
i'm developer!
senior
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With