Wednesday, 23 January 2019

Why don't the interfaces in Level 2 inherit from the interfaces in Level 1

Why don't the interfaces in Level 2 inherit from the interfaces in Level 1?

Answer:

In Level 2 we needed to add some more functionality to, for example, the Node interface. There are several choices for how to add new functionality to an existing interface. One is to define a new interface, say Node2, and have it inherit from Node, adding the new methods. Another possibility is to have the Node2 interface copy all the existing methods from Node, rather than inheriting them. Another method is to extend the interfaces.
All three of these methods have advantages and disadvantages, which vary according to the language binding you are using. One big disadvantage of the inheritance method is the diamond inheritance you get when, for example, Document2 is created, which inherits both from Document (which inherits from Node) and from Node2. But Node2 also inherits from Node. The problem will only get worse as we design Level 3, Level 4, etc of the DOM. This inheritance also necessitates a lot of casting, and the user has to know which precise interface a method was defined on, to know what to cast the result to.
Copying all the methods leads to bloated interfaces, since many methods will be present many times (again, this problem gets worse as we design more Levels of the DOM).

Adding new methods to existing interfaces where appropriate does not work in all languages, but those languages which need a different way of doing things are expressly allowed to do so by the DOM specification. It avoids the problems of diamond inheritance and excessive casting, and cuts down on interface bloat.

No comments:

Post a Comment