April 2018
Beginner
536 pages
13h 21m
English
Polymorphism is the ability to present the same interface for differing underlying forms (data types). Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance.
Polymorphism is what enabled us to implement the LSP in the preceding section:
class AreaCalculator {
public area(shapes: Shape[]) {
return shapes.reduce(
(p, c) => p + c.area(),
0
);
}
}
Objects of the derived class (Circle and Rectangle) may be treated as objects of a base class (Shape) in places such as method parameters (such as the area method). Base classes may define and implement abstract methods, and derived classes can override them, which means they provide their definition and implementation. ...