Parametric polymorphism and overload in Java and C++

Asked

Viewed 828 times

8

The following question fell in IFSP’s tender procedure:

In Java and C++ programming languages, parametric polymorphism is materialized, respectively, by the functionalities and/or characteristics:
(A) Generic and Templates.
(B) Generic and Overload of Operators.
(C) Overload and Templates.
(D) Annotations and Superscript.

In the preliminary template the correct option is "A".

I marked the letter "C", because I understand that parametric polymorphism (possibility to define several functions with the same name, but with different parameters) and even polymorphism by overload.

1 - What is the difference between parametric polymorphism and overload polymorphism?)

2 - In C++ Templates is the way to define parametric polymorphism?

3 - What is the way to define parametric polymorphism in Java? It would be Generics? In this case, we could translate the word and say that it is generic?

2 answers

10


The right one is the same.

1 - What is the difference between parametric polymorphism and overload polymorphism?)

Behold Overload method is polymorphism?. And + 3 overloads - What would that be?. I also replied in Polymorphism in procedural language.

Both are solved at compile time and are used in static typing. But they are different mechanisms. The parametric generates new code according to the type used and there may be an explosion of generation or ends up creating a certain dynamism using void *, despite maintaining the safety of types on the surface. Overload is always made a choice between existing methods.

The parametric is much more powerful and can be used in various circumstances (it comes to be Turing complete), especially when using template, the overload can only occur in methods.

2 - In C++ Templates is the way to define parametric polymorphism?

Yes. You can see in What is the difference between "Generics" (Java/C#) and "template" (C++).

3 - How to define parametric polymorphism in Java? Would Generics be? In this case, we could translate the word and say it is generic?

Yes. As shown in link above. Usually when we translate it is as generic as well as templates are templates, but we actually use the term more in English. See more in What is generic programming?.

That is, if you consulted more Sopt could have hit :) I keep telling this to everyone.

  • 1

    It would not be the case to mark as duplicate?

  • @Guilhermecostamilam I was putting the difference that there is nowhere.

1

I put there, in the comments, links to sources that talk about parametric polymorphism. Overload refers to "regular" polymorphism, so option C is incorrect.

Parametric polymorphism means that one type can be parameterized by another type - for example, the collections in Java (List, for example) can be composed of objects of different types. When you declare a ArrayList, you always define a guy ArrayList<ClassType>. Similarly, you can parameterize in C++, with templates.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.