Late binding leads to one interesting consequence. You can call methods and properties defined in descendants from a base class. And the descendants themselves may not even exist. Late binding is late because the check only happens when this code is in use.
This feature is used in the template method pattern. It applies when subclasses have a common logic that relies in part on the behavior of subclasses. This logic is implemented in the method from the base class, and the part that differs (for each subclass) is delegated to the descendants.
Let's take our tags as an example.
class HTMLElement {
// implementation of all methods of the parent class
// https://hexlet.io/courses/js-classes/lessons/inheritance/theory_unit
}
Look at the toString()
method. You can see that its code will remain identical for most tags. The only thing that changes is the name of the tag itself.
class HTMLAnchorElement extends HTMLElement {
toString() {
// Parent method
const attrLine = this.stringifyAttributes();
// Parent method
const body = this.getTextContent();
return `<a${attrLine}>${body}</a>`;
}
}
We can modify the code so that the toString()
method moves to the HTMLElement. And the only thing that will be left to the subclasses is the name of the tag.
class HTMLElement {
toString() {
const attrLine = this.stringifyAttributes();
const body = this.getTextContent();
// getTagName is a method that all subclasses must implement
const tagName = this.getTagName();
return `<${tagName}${attrLine}>${body}</${tagName}>`;
}
}
The resulting code is better than the original version, as it significantly reduces duplication (the tags are about 100 pieces!). But there's one catch. Tags are always single tags, so the current version of toString()
won't work for them. This situation can be resolved in various ways, such as through inheritance.
Create two subclasses for the HTMLElement: one HTMLSingleElement and one HTMLPairElement. Now, specific tags classes must be inherited from one of the specified classes. Each of these classes will have its own implementation of the toString()
method.
class HTMLSingleElement extends HTMLElement {
toString() {
const attrLine = this.stringifyAttributes();
// getTagName is a method that all subclasses must implement
const tagName = this.getTagName();
// A single tag is created
return `<${tagName}${attrLine}>`
}
}
class HTMLPairElement extends HTMLElement {
toString() {
const attrLine = this.stringifyAttributes();
const body = this.getTextContent();
// getTagName is a method that all subclasses must implement
const tagName = this.getTagName();
return `<${tagName}${attrLine}>${body}</${tagName}>`;
}
}
Despite the differences in the implementation of toString()
, both of these subclasses require their descendants to implement the same getTagName()
method.
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.