Question
In the context of this exercise, can static methods only be called on the class that defined it?
Answer
No, you can also call a static method in the class’s subclasses.
In the exercise example, it defined an Animal
class. If we created a subclass of Animal
, then it would inherit the static method generateName()
, and you would be able to call the method from that subclass.
For example,
class Dog extends Animal {
...
}
/* The static method is inherited from Animal, and
we can call it on the Dog class. */
console.log(Dog.generateName());