pzaj
pzaj

Reputation: 1092

Interface inheritance combined with class inheritance

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!

Upvotes: 2

Views: 308

Answers (3)

DaniCE
DaniCE

Reputation: 2421

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.

Upvotes: 1

Enigmativity
Enigmativity

Reputation: 117175

This is what you want:

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

Upvotes: 2

Gary McGill
Gary McGill

Reputation: 27556

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
{
}

Upvotes: 7

Related Questions