Polymorphism

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. ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.