Taking advantage of the link that @Bacco commented: https://dicionario.priberam.org/ad%20hoc
(latin speech which means "for this") which is intended to an end
specific.
I’ve heard that term often related to cheese database, which gives to understand well its application also in computer:
Any system allows consulting a product by name, so we have a "generic" query that is applied to all product queries by name, which even because it is so, can be encapsulated in a stored Procedure:
SELECT * FROM PRODUTO WHERE NOME='BATATAS'
Where we only inform the value of "POTATOES". Now if you have a situation that needs to consult a product by name, but also a product that is active, that has been registered on a specific date, etc., that would be a query Ad hoc, ie, will be used for a specific purpose, different from the "generic" query that has a "general" purpose".
About Ad hoc polymorphism, the concept would be the same, a "specific polymorphism" or "for a certain purpose". And how would that be?
Well, polymorphism (or many forms) is one of the object orientation concepts where a method can behave differently, having the same contract, in different classes.
We can apply polymorphism with interfaces or only classes:
public class Calculo
{
public int Calcular(int num1, int num2)
{
return num1 + num2;
}
}
public class CalculoAlternativo : Calculo
{
public int Calcular(int num1, int num2)
{
return num1 - num2;
}
}
Here we see the polymorphism of the method Calcular
, it has the same signature (name and parameters), but behaves differently in the class Calculo
and CalculoAlternativo
.
The polymorphism ad hoc is one that is created for a specific purpose, that is, the parameters are different and it must have a different behavior to meet a specific purpose. This can be implemented through overload, or overloading:
public class Calculo
{
public int Calcular(int num1, int num2)
{
return num1 + num2;
}
public int Calcular(int num1, int num2, bool soPositivo)
{
var resultado = num1 - num2;
if (soPositivo && resultado < 0)
{
resultado = 0;
}
return resultado;
}
}
In this case, the method has the same name, but behaves differently, and takes different parameters.
References:
https://www.geeksforgeeks.org/ad-hoc-inclusion-parametric-coercion-polymorphisms/
http://www.btechsmartclass.com/java/java-polymorphism.html
https://catonmat.net/cpp-polymorphism
I think a good start is to understand the locution. https://dicionario.priberam.org/ad%20hoc - ad-hoc is "specific purpose", "specific purpose".
– Bacco