Casa » Polimorfismo (programmazione)

Polimorfismo (programmazione)

1967
  • Christopher Strachey
Computer programming workspace demonstrating polymorphism with code snippets.

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

UNESCO Nomenclature: 1203
- Informatica

Tipo

Sistema astratto

Interruzione

Incrementale

Utilizzo

Uso diffuso

Precursori

  • il concetto di puntatori di funzione in linguaggi come c
  • concetti di programmazione generica
  • ereditarietà, che fornisce la gerarchia di classi necessaria per il polimorfismo dei sottotipi
  • the need for flexible and extensible software sistemi

Applicazioni

  • drawing different shapes (‘circle’, ‘square’) using a single ‘draw()’ method call
  • processing various document types (‘pdf’, ‘word’) with a generic ‘print()’ function
  • implementing user interface event handlers
  • raccolte generiche che possono contenere oggetti di tipi diversi ma correlati
  • framework di iniezione di dipendenza

Brevetti:

NA

Potenziali idee innovative

Livelli! Iscrizione richiesta

Per accedere a questo contenuto devi essere un membro di !Professionals (100% free)!

Iscriviti ora

Siete già membri? Accedi
Related to: polymorphism, overriding, dynamic dispatch, late binding, subtype, interface, abstraction, oop, method overriding, runtime.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

DISPONIBILE PER NUOVE SFIDE
Ingegnere meccanico, responsabile di progetto, ingegneria di processo o ricerca e sviluppo
Sviluppo efficace del prodotto

Disponibile per una nuova sfida con breve preavviso.
Contattami su LinkedIn
Integrazione di componenti elettronici in plastica e metallo, progettazione in base ai costi, GMP, ergonomia, dispositivi e materiali di consumo di medio-alto volume, produzione snella, settori regolamentati, CE e FDA, CAD, Solidworks, Lean Sigma Black Belt, ISO 13485 in ambito medico

Stiamo cercando un nuovo sponsor

 

La tua azienda o istituzione si occupa di tecnica, scienza o ricerca?
> inviaci un messaggio <

Ricevi tutti i nuovi articoli
Gratuito, no spam, email non distribuita né rivenduta

oppure puoi ottenere la tua iscrizione completa -gratuitamente- per accedere a tutti i contenuti riservati >Qui<

Contesto storico

(se la data non è nota o non è rilevante, ad esempio "meccanica dei fluidi", viene fornita una stima approssimativa della sua notevole comparsa)

Principi di invenzione, innovazione e tecnica correlati

Torna in alto

Potrebbe anche piacerti