Log in Sign up
Back to Discover
💻

Polymorphism (computer science)

technology Maturity 7-9

Computers can do many things. A rule can work in different ways. It can work for numbers. It can work for words. This helps the computer stay smart. It is a neat trick! Can you think of a rule like that?

41 words

Computers use special rules. These rules can work in many ways. This is called polymorphism.

UML class pet.svg
UML class pet.svg

One rule can work with numbers. The same rule can work with words. The rule changes how it acts. It sees what you give it. Then it knows what to do.

Some rules are like a group. A rule can work for any pet. It can work for a cat. It can also work for a dog.

UML class pet.svg
UML class pet.svg

This helps computers stay smart. It lets one rule do many jobs. It is a very neat trick!

96 words

In computer science, polymorphism is a way to use one rule for many types. The word comes from biology. In nature, one species can have many different forms. In programming, it lets a single name or tool work with different kinds of data.

There are three main ways this works. First is ad hoc polymorphism. This is when a rule acts differently depending on the data. For example, a rule might add numbers but join words together. It sees the type and chooses the right path.

Second is parametric polymorphism. This uses symbols to stand in for any type. It lets a function work the same way for everything. A list of numbers and a list of words can use the same rule to find their length. This makes code very flexible.

Third is subtyping. This happens when types belong to a group.

UML class pet.svg
UML class pet.svg
In the diagram, a cat and a dog are both pets. A rule that works for a pet will also work for a cat. This is called inclusion polymorphism.

Computers can do this in two ways. They can decide the rule early, which is called static polymorphism. This is fast. Or they can decide while the program runs. This is called dynamic polymorphism. It is more flexible but can be slower.

217 words

Polymorphism is a very useful idea in computer science. It allows a single value or variable to have more than one type. It also lets one operation work on many different types of data. The word itself comes from a principle found in biology. In nature, a single species can have many different forms or stages. In programming, this concept helps people write code that is much more flexible. It allows a single interface to work with many different kinds of data.

There are three main ways that polymorphism works. The first way is called ad hoc polymorphism. This happens when a function can take different types of arguments but acts differently for each one. For example, an "add" function might sum two numbers but join two words together. The second way is parametric polymorphism. This method uses abstract symbols instead of specific types. This lets a function handle values in a uniform way without needing to know their exact type. The third way is subtyping, which is also called inclusion polymorphism.

UML class pet.svg
UML class pet.svg
In this version, a name refers to many different classes that all belong to one common superclass.

People have studied these ideas for a long time. Christopher Strachey described the first two types in his book, "Fundamental Concepts in Programming Languages." He called ad hoc and parametric polymorphism the two main classes. Ad hoc polymorphism was a feature of a language called ALGOL 68. Meanwhile, parametric polymorphism was a core part of the ML language system. Later, in 1985, Peter Wegner and Luca Cardelli introduced the term "inclusion polymorphism." They used it to model how subtypes and inheritance work together. They noted that the Simula language was the first to use it.

Many different programming languages use these rules today. In Java, you can see ad hoc polymorphism when using different add functions. In the language Haskell, parametric polymorphism is used for things like lists. The language C++ uses templates to achieve this same goal. Other languages like C#, Delphi, Go, and Java use something called generics. Subtyping can be seen in how a computer handles different animals.

UML class pet.svg
UML class pet.svg
If a rule works for a general "Pet," it will also work for a "Cat" or a "Dog." This is because cats and dogs are subtypes of pets.

Computers can handle polymorphism in two different ways. One way is called static polymorphism, which happens at compile time. This method is very fast because the computer decides the rules early. The other way is called dynamic polymorphism, which happens while the program is running. This is more flexible and allows for things like "duck typing." While dynamic polymorphism is slower, it lets a library work with objects without knowing their full type. Both ways help programmers solve hard jobs in many different ways.

468 words

Polymorphism is a fundamental concept in programming language theory and type theory. It allows a single value or variable to possess more than one type. This concept also enables a single operation to be performed on values of different types. In object-oriented programming, polymorphism provides one interface for entities of different data types. The term is borrowed from biology, where an organism or species can have many different forms or stages. By using polymorphism, programmers can write more flexible and expressive code.

There are three major recognized forms of polymorphism. The first is ad hoc polymorphism, which defines a common interface for an arbitrary set of individually specified types. The second is parametric polymorphism, which does not specify concrete types but uses abstract symbols to substitute for any type. The third is subtyping, also known as subtype or inclusion polymorphism. In subtyping, a name denotes instances of many different classes that are related by a common superclass.

UML class pet.svg
UML class pet.svg

Ad hoc polymorphism occurs when functions can be applied to arguments of different types but behave differently depending on the specific type. This is often called function overloading or operator overloading. For example, in the Java language, an "add" function might sum two integers but join two strings together. These are considered entirely distinct functions by the compiler. In some dynamically typed languages, the correct function might only be determined at run time. Another related form is coercion polymorphism, which involves implicit type conversion.

Parametric polymorphism allows a function or data type to be written generically. This means it can handle values uniformly without depending on their specific type. This method increases a language's expressiveness while maintaining full static type safety. It applies to both data types and functions. A function that can be applied to values of different types is a polymorphic function. A data type that appears to be a generalized type, such as a list of arbitrary elements, is a polymorphic data type. This concept is ubiquitous in functional programming. John C. Reynolds and Jean-Yves Girard formally developed this idea as an extension to lambda calculus, known as System F.

Subtyping allows a function to be written for a specific type, T, while still working for a subtype, S. This follows the Liskov substitution principle. In this relationship, T is the supertype and S is the subtype.

UML class pet.svg
UML class pet.svg
For instance, if a function accepts a "Pet," it will also work if passed a "Cat" or a "Dog." In object-oriented programming, this is often achieved through subclassing or inheritance. Each class typically contains a virtual table, or vtable, which is a table of functions for the class interface. Each object contains a pointer to this vtable. This mechanism allows for late binding, where calls are not bound until the time of invocation.

Polymorphism can be categorized by when the implementation is selected. Static polymorphism occurs at compile time and is known as static dispatch. It executes faster because there is no dynamic dispatch overhead. It also allows for better static analysis and optimization by compilers. Dynamic polymorphism occurs at run time and is known as dynamic dispatch. While it is slower, it offers more flexibility. It allows for features like duck typing and lets libraries operate on objects without knowing their full types. Most subtype polymorphism uses dynamic dispatch, while ad hoc and parametric polymorphism typically use static dispatch.

History shows that interest in these systems grew significantly during the 1990s. Practical implementations began appearing by the end of that decade. Christopher Strachey originally described ad hoc and parametric polymorphism in his work, "Fundamental Concepts in Programming Languages." He identified them as the two main classes of polymorphism. Ad hoc polymorphism was a feature of ALGOL 68, while parametric polymorphism was central to the ML type system. In 1985, Peter Wegner and Luca Cardelli introduced the term "inclusion polymorphism" to model subtypes. They cited Simula as the first language to implement this concept.

657 words
🖼️ Images & Media (1)
File:UML class pet.svg
UML class pet.svg
Up Next
💻
Operator overloading
Technology
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.