3

Consider the following UML diagram:

enter image description here

In a nutshell:

  • ChildClass1 and ChildClass2 use all of the members of ParentClass.
  • ChildClass3only uses Member1 and Member2 and the value of Member5 is a constant value. (0)

What would be the best way to implement this behaviour?

At the moment, I've inherited the class as normal and overridden Member3 and Member4 to throw NotSupportedException and made Member5 read-only and return 0 all the time.

I'm not convinced this is the best idea as Member3 and Member4 are irrelevant to ChildClass3.

Other things I've considered:

  • Instead of inheriting. Creating a ParentClass inside ChildClass3 and then exposing only the properties I need.

  • Using interfaces, but this got too messy, too quickly.

  • Restructuring the hierarchy, but I didn't really look at this in too much detail.

Is there any other options?

For context: I tried to keep this simple to avoid walls of code, but this appears to have lead to confusion. Oops =/

ParentClass in this case is Person, with the following fields:

  • Forename : string
  • Surname : string
  • Gender: string
  • DateOfBirth : Nullable<DateTime>
  • Location: int

Now in most cases, the child classes (Employee, Manager, etc.) use all of these fields. However one class (Visitor), I only need to know a name, I don't care about their age or location. Location in this case is set to 0. I know it's a magic number, but it's important to the system.

I can understand how this violates LSP, but at the same time, I don't really think having interfaces to split a name and other personal information makes a lot of sense. Naming this would give me a headache too.

In the other classes, there's interfaces everywhere; IMedicalData, IContactInformation, etc.

I only chose inheritance in this case since a visitor is technically a type of person.

3
  • 2
    You haven't told us anything about the behavior these classes implement, what these five members are, or why two of them are irrelevant to this new derived class you're contemplating. I don't think we can give you meaningful advice without that information. When the question is phrased this abstractly the only correct answer is "it depends".
    – Ixrec
    Apr 13, 2016 at 10:20
  • "Restructuring the hierarchy, but I didn't really look at this in too much detail." - I think this is the key statement here. I think this is the way to go, most probably Apr 13, 2016 at 10:32
  • 1
    I've added a bit more context to the question, hopefully this clears up any ambiguity.
    – Jake
    Apr 13, 2016 at 11:03

2 Answers 2

7

Usually you should do it this way:

uml

This is the most canonical way of solving your problem. In general you add behaviors and states when subclassing

4

At the moment, I've inherited the class as normal and overridden Member3 and Member4 to throw NotSupportedException and made Member5 read-only and return 0 all the time.

I'm not convinced this is the best idea as Member3 and Member4 are irrelevant to ChildClass3.

It isn't the best idea: this is a classic example of violating the Liskov Substitution Principle.

The simple answer to how best to implement it is "don't use inheritance; use interfaces & composition as required".

The more detailed answer has to make some guesses about what Member[1-5] are, but something like:

public interface IMember12And5
{
    Member1;
    Member2;
    Member5;
}

public interface IAllMembers : IMember12And5
{
    Member3;
    Member4;
}

public class ChildClass1 : IAllMembers  { ... }

public class ChildClass2 : IAllMembers  { ... }

public class ChildClass3 : IMember12And5 { ... }

might work. Alternatively, compose ChildClass[1-2] from Member12And5 and Member3And4 and have ChildClass3 composed only of Member12And5.

Without more detail of what the members really are, a better answer cannot be offered.

1
  • Was away from the desk. I'll amend the question now.
    – Jake
    Apr 13, 2016 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.