2

I'm having a problem with a task given and I would like to ask for an advice.

I've been told to implement: IPerson interface Person class based on IPerson IEmployee and Employee extending IPerson and Person

The last sentence made by confused, I have no idea how to cope with that requirements. I have a thought and I'd like it to be verified by You, since Person and Employee will be stored using a List then:

Will IEmployee extend IPerson and Employee just implement IEmployee? or in addition to that Employee should extend Person as well?

Thanks for an answer to this question!

4
  • As an addition: Remember that C# doesn't support multi inheritance. A class can only extend one class, but it can implement n-interfaces. Commented Oct 15, 2015 at 13:19
  • Just give IPerson a string Name property and IEmployee an string EmployeeCode property and see how far you'll get? Commented Oct 15, 2015 at 13:21
  • @COdeCaster you're right I should have done that. I just wanted to ask before implementing it and most importantly to confirm my way of thinking and I believe both ways (with extending Person and not) would work as long as they would implement the same interfaces, but then Employee would need to repeat what's done in Person. Commented Oct 15, 2015 at 13:25
  • 1
    That last point is exactly what I wanted you to find out for yourself. Commented Oct 15, 2015 at 13:26

3 Answers 3

7

All employees are people, and so all employees implement both IEmployee and IPerson. The Employee class extends the Person class.

public interface IPerson
{
}

public class Person : IPerson
{
}

public interface IEmployee : IPerson
{
}

public class Employee : Person, IEmployee
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think Employee should extend Person, that's how I understood OP
This answer was first, so I'm gonna mark it as the proper answer. Thank you for confirmation of my thoughts.
2

This is what you want:

public interface IPerson { }
public interface IEmployee : IPerson { }
public class Person : IPerson { }
public class Employee : Person, IEmployee { }

3 Comments

"give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime". Why is that what OP wants?
@CodeCaster - I thought this directly answered the OP.
Sure it does, but there's no explanation as of why it does. I don't think "use this code, it'll solve your problem" is a helpful answer.
1

The question really is that you should think twice before implement interface hierarchies...

You don't want to force interface implementation classes to implement methods that will be not used, so think if methods that use IEmployee as parameters will also need all IPerson properties or not. If not is better to define a new interface with just the needed properties.

This corresponds to Interface segregation of SOLID principles.

5 Comments

And that's where OO starts to become fuzzy when you try to teach it with too abstract or vague requirements. Sure, an "Employee" always is a "Person" in everyday language, but there is a reason this IEmployee inteface requirement was created in the first place. If this example is for a payment system, IEmployee is plain wrong. You can for example have to pay cleaners (which you hire through a company), or hire self-employed people. IPayableEntity would then make way more sense, as you don't deal with those entities as people anyway, so (I)Person has no place in that inheritance tree.
I don't think my explanation is too abstract, but I have updated the link to a "less abstract" one with a detailed example. (by the way, your example is very good...)
I didn't mean your example, but the issue with talking about OO design in general.
so we could not talk about OO design?
I am not criticizing your post, I am adding my view to it. In general, OO examples are too simplistic. Yes, an Employee is-a Person, so it could inherit from it, and everyone nods in understanding. And a Car has a has-many relation with its Wheels, so that should be a Car's properties, and everyone nods again in agreement. Those hardly ever touch the real life problems you encounter in OO design though, which I tried to demonstrate. OP's problem statement as such has only one correct answer, to criticize the design more, we need to know what IPerson and IEmployee are supposed to mean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.