Polymorphism
You can override an inherited method which opens the door to polymorphism.
class CourseAssistant { getBaseSalary() { return 500.0; //dollars } getHourlyPayRate() { return 15.0; // dollars } } class ExperiencedCourseAssistant extends CourseAssistant { /* overrides */ getHourlyPayRate() { return 1.1 * super.getHourlyPayRate(); } } function calcPayment(courseAssistant, hoursWorked) { let wages = courseAssistant.getBaseSalary() + hoursWorked * courseAssistant.getHourlyPayRate(); /* dynamic dispatch */ console.log(wages); } const tom = new CourseAssistant(); const mona = new ExperiencedCourseAssistant(); calcPayment(tom, 10); calcPayment(mona, 10);
In the example above, getHourlyPayRate()
is dispatched based on the actual "type" of the courseAssistant
argument. It will decide on dispatching the overloaded getHourlyPayRate()
during runtime, which is a (dynamic) polymorphic behavior.
Method overriding in JavaScript means that you can have methods with the same name in parent and subclasses.
Aside: JavaScript does not support method overloading since the number of arguments to a method can be fewer/more than declared parameters.