2

I have three classes: CookedItem : ConsomableItem : Item

Item implements this method:

protected void Initialize(string name,TypeItem type) {...}

ConsomableItem implements this method:

protected void Initialize(float duration,string name,TypeItem type) {...}

Now here is my problem. The correct method to call for CookedItem is ConsomableItem.Initialize().

Can I hide the method Item.Initialize()to only see the methodConsomableItem.Initialize()`?

I can't use override because the signatures are different.

Any suggestion?

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
CrymX
  • 39
  • 2

2 Answers2

2

You are violating the third principal of the SOLID. The Liskov Substitution Principal. Have a look at here to understand what it is.

Basically you have built a wrong inheritance.

What you should be doing is to ICookable and IConsumable interfaces, as behaviours. Then you will have two different item class implementations(you may still abstract them in an abstract class).

Community
  • 1
  • 1
Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
0

In your class hierarchy Initialize methods are protected, so they won't be visible to classes that are not in this inheritance hierarchy, so it is up to you to design a better API for external clients.

As for hiding the methods inside the hierarchy, I don't think it's possible, even if the methods were virtual, compiler would not allow you to change access modifiers of the methods. To find out more about why it's disallowed, read here

In addition to breaking LSP as @Tolga Evcimen mentioned, it does not make a lot of sense to have this inheritance hierarchy. Inheritance is thought of as is-a relationship, now is CookedItem really an Item, if calling Initialize methods that belong to item do not make sense / yield inconsistent object state?

Community
  • 1
  • 1
ironstone13
  • 3,325
  • 18
  • 24