Polymorphism, from Greek for “many shapes,” allows objects of different classes to be treated as objects of a common superclass. It enables a single interface, such as a method name, to be used for a general class of actions. The specific action is determined by the exact type of the object at runtime. This is often achieved through method overriding.
Polymorphism (programming)
- Christopher Strachey
Polymorphism is a powerful concept that allows for writing flexible and decoupled code. The most common form in OOP is subtype polymorphism, which is enabled by inheritance and méthode overriding. When a subclass provides a specific implementation for a method that is already defined in its superclass, it is called overriding. A variable of the superclass type can refer to an object of any of its subclasses. When a method is called on this variable, the version of the method that gets executed is the one belonging to the object’s actual class, not the variable’s type. This decision is made at runtime, a process known as dynamic dispatch or late binding.
For example, consider a superclass ‘Shape’ with a method ‘calculateArea()’. Subclasses like ‘Circle’ and ‘Rectangle’ can inherit from ‘Shape’ and provide their own specific implementations of ‘calculateArea()’. A program can then have a list of ‘Shape’ objects, containing both ‘Circle’ and ‘Rectangle’ instances. When iterating through the list and calling ‘calculateArea()’ on each element, the correct formula will be used for each shape automatically. This eliminates the need for long ‘if-else’ or ‘switch’ statements to check the type of each object, leading to cleaner, more extensible code. New shapes can be added to the system without modifying the code that processes them.
Taper
Perturbation
Usage
Précurseurs
- the concept of function pointers in languages like c
- generic programming concepts
- inheritance, which provides the class hierarchy necessary for subtype polymorphism
- the need for flexible and extensible logiciel systèmes
Applications
- drawing different shapes (‘circle’, ‘square’) using a single ‘draw()’ method call
- processing various document types (‘pdf’, ‘word’) with a generic ‘print()’ function
- implementing interface utilisateur event handlers
- generic collections that can hold objects of different but related types
- dependency injection frameworks
Brevets:
Idées d'innovations potentielles
!niveaux !!! Adhésion obligatoire
Vous devez être membre de l'association pour accéder à ce contenu.
DISPONIBLE POUR DE NOUVEAUX DÉFIS
Ingénieur mécanique, chef de projet, ingénierie des procédés ou R&D
Disponible pour un nouveau défi dans un court délai.
Contactez-moi sur LinkedIn
Intégration électronique métal-plastique, Conception à coût réduit, BPF, Ergonomie, Appareils et consommables de volume moyen à élevé, Production allégée, Secteurs réglementés, CE et FDA, CAO, Solidworks, Lean Sigma Black Belt, ISO 13485 médical
Nous recherchons un nouveau sponsor
Votre entreprise ou institution est dans le domaine de la technique, de la science ou de la recherche ?
> envoyez-nous un message <
Recevez tous les nouveaux articles
Gratuit, pas de spam, email non distribué ni revendu
ou vous pouvez obtenir votre adhésion complète - gratuitement - pour accéder à tout le contenu restreint >ici<
Inventions, innovations et principes techniques connexes