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 Verfahren 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.
Typ
Unterbrechung
Verwendung
Vorläufersubstanzen
- 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 Software Systeme
Anwendungen
- 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
- generic collections that can hold objects of different but related types
- dependency injection frameworks
Patente:
Mögliche Innovationsideen
!Professionals (100% free) Mitgliedschaft erforderlich
Sie müssen ein Professionals (100% free) Mitglied sein, um auf diesen Inhalt zugreifen zu können.
VERFÜGBAR FÜR NEUE HERAUSFORDERUNGEN
Maschinenbauingenieur, Projekt-, Verfahrenstechnik- oder F&E-Manager
Kurzfristig für eine neue Herausforderung verfügbar.
Kontaktieren Sie mich auf LinkedIn
Integration von Kunststoff-Metall-Elektronik, Design-to-Cost, GMP, Ergonomie, Geräte und Verbrauchsmaterialien in mittleren bis hohen Stückzahlen, Lean Manufacturing, regulierte Branchen, CE und FDA, CAD, Solidworks, Lean Sigma Black Belt, medizinische ISO 13485
Wir suchen einen neuen Sponsor
Ihr Unternehmen oder Ihre Institution beschäftigt sich mit Technik, Wissenschaft oder Forschung?
> Senden Sie uns eine Nachricht <
Erhalten Sie alle neuen Artikel
Kostenlos, kein Spam, E-Mail wird nicht verteilt oder weiterverkauft
oder Sie können eine kostenlose Vollmitgliedschaft erwerben, um auf alle eingeschränkten Inhalte zuzugreifen >Hier<
Verwandte Erfindungen, Innovationen und technische Prinzipien