Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain polymorphism

What is polymorphism? I'm not sure I am understanding it correctly.

In the Python scope, what I am getting out of it is that I can define parameters as followed:

def blah (x, y)

without having to specify the type, as opposed to another language like Java, where it'd look more along the lines of:

public void blah (int x, string y)

Am I getting that right?

like image 625
TopChef Avatar asked Jul 23 '10 21:07

TopChef


People also ask

What you mean by polymorphism?

Definition of polymorphism : the quality or state of existing in or assuming different forms: such as. a(1) : existence of a species in several forms independent of the variations of sex. (2) : existence of a gene in several allelic forms also : a variation in a specific DNA sequence.

What is polymorphism & its types?

Types of Polymorphism in Oops In Object-Oriented Programming (OOPS) language, there are two types of polymorphism as below: Static Binding (or Compile time) Polymorphism, e.g., Method Overloading. Dynamic Binding (or Runtime) Polymorphism, e.g., Method overriding.

What is the polymorphism in OOPs?

Polymorphism is one of the core concepts of object-oriented programming (OOP) and describes situations in which something occurs in several different forms. In computer science, it describes the concept that you can access objects of different types through the same interface.

What is polymorphism explain in Java?

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks.


1 Answers

Beware that different people use different terminology; in particular there is often a rift between the object oriented community and the (rest of the) programming language theory community.

Generally speaking, polymorphism means that a method or function is able to cope with different types of input. For example the add method (or + operator) in the Integer class might perform integer addition, while the add method in the Float class performs floating-point addition, and the add method in the Bignum class performs the correct operations for an arbitrary-size number. Polymorphism is the ability to call the add method on an object without knowing what kind of a number it is.

  • One particular kind of polymorphism, usually called parametric polymorphism in the functional community and generic programming in the OOP community, is the ability to perform certain operations on an object without caring about its precise type. For example, to reverse a list, you don't need to care about the type of the elements of the list, you just need to know that it's a list. So you can write generic (in this sense) list reversal code: it'll work identically on lists of integers, strings, widgets, arbitrary objects, whatever. But you can't write code that adds the elements of a list in a generic way, because the way the elements are interpreted as numbers depends on what type they are.

  • Another kind of polymorphism, usually called ad-hoc polymorphism or (at least for some forms of it) generic programming in the functional community, and often subtyping polymorphism (though this somewhat restricts the concept) in the OOP community, it the ability to have a single method or function that behaves differently depending on the precise type of its arguments (or, for methods, the type of the object whose method is being invoked). The add example above is ad-hoc polymorphism. In dynamically typed languages this ability goes without saying; statically-typed languages tend to (but don't have to) have restrictions such as requiring that the argument be a subclass of some particular class (Addable).

Polymorphism is not about having to specify types when you define a function. That's more related to static vs. dynamic typing, though it's not an intrinsic part of the issue. Dynamically typed languages have no need for type declarations, while statically typed languages usually need some type declarations (going from quite a lot in Java to almost none in ML).

like image 172
Gilles 'SO- stop being evil' Avatar answered Oct 07 '22 14:10

Gilles 'SO- stop being evil'