DEV Community

Cover image for Polymorphism Explained: How Object-Oriented Code Stays Flexible
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Polymorphism Explained: How Object-Oriented Code Stays Flexible

This article was originally published on bmf-tech.com.

Overview

Summarizing polymorphism

What is Polymorphism

  • Poly (multiple) Morphism (forms)
  • Providing a shared interface for multiple different types
  • Instances behave differently depending on the class when receiving arguments

Types of Polymorphism

  • Ad-hoc polymorphism
    • Functions have different implementations for arguments of different types
    • Achieved through overloading and function overloading
  • Parametric polymorphism
    • Code is written without specifying types and can be used transparently for each type
    • Applicable to various types by passing the type as an argument
    • Achieved through generics or templates
  • Subtyping
    • Handling multiple types with a common supertype under a single name

References

Top comments (0)