Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit Class1 and implement Interface1 both in case Class1 already implements Interface1

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"
    }
}
like image 464
Nikhil Vartak Avatar asked Nov 26 '15 06:11

Nikhil Vartak


People also ask

How many interfaces can a class inherit?

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:

How to implement two interfaces in a single class?

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.

How does a derived class implement an interface in Java?

} } 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).

Should fields/properties be inherited from a class or an interface?

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.


1 Answers

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)


first thought: case 2 is allowed at least because interfaces can be implemented explicitly (sample). it turned out to be a subset of available options
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
like image 66
ASh Avatar answered Oct 04 '22 04:10

ASh