0

In prototype-based programming one creates new objects by cloning a prototype. In the new object one can change the behavior by adding, removing or modifying methods and data. In my lecture notes it is written that a method call is achieved by dynamic dispatch, i.e. when a method on an object is called, we look whether the method is available in the object and if not, the request is delegated to its parent objects. How do we achieve method removal? Does the object have a list of removed methods which should not be delegated to parent objects? I consider adding and modifying as more natural in this spirit of delegation as removal...

Sebastian Bechtel
  • 363
  • 1
  • 3
  • 12

1 Answers1

0

Are you talking about any specific programming language?

As a concept, it doesn't make sense to delete a method from a subclass that is avaliable on its superclass. You could change the behavior in a specific way but not remove functionality.

There are ways to force an error if you call a "removed" method, see here Disabling inherited method on derived class for a java example and a interesting discussion. But In my opinion if you really need to remove a method probably you have a bad classes design.

Community
  • 1
  • 1
  • Remember that the question is about prototype based languages. There is no class hierarchy. – Sebastian Bechtel Jul 01 '16 at 11:50
  • Agree, I shouldn't speak about classes and subclasses. But in prototypal inheritance there is also a hierarchy (defined by the way the objects link to each others). As I said I dont think that you should use a prototype instance of an object if you don't need all the features of that object. It's a better idea to split the prototype object or use one more level of inheritance. I find very instructive the example of @Chandu about Shape class. – Marco A. Hernandez Jul 01 '16 at 13:03