mardi 28 avril 2015

What would be the correct design here (encapsulation, polymorphism, inheritance)?

Assuming I have an interface and 3 or 4 classes that implement the interface. Now let's say I have another method but it is relevant only for one or two implementing classes.

So as I see it there are two approaches

  1. Declare the method in the interface, implement in all the classes. In classes that the method is not relevant - throw "not supported" exception.
  2. Declare the method only in the implementing classes where relevant but then you have to downcast the instance from interface type to the specific type in order to call the method. So would be your design here and why?

Aucun commentaire:

Enregistrer un commentaire