Duck Typing is a typing concept in which the type or class of an object is determined by its methods and properties rather than its explicit inheritance or implementation of an interface.
The term duck typing comes from the phrase:
If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck
That is, if an object has the methods and properties we need, we can treat it as if it were the type we expect, regardless of its actual class.

In programming, this translates to Duck Typing, the type of an object is not defined by its class, but by its behavior.
It is a predominant concept in dynamic languages like Python or JavaScript, and represents a way of viewing data types very different from that of strict languages like C++ or Java.
The Philosophy: Behavior vs Identity
To understand it, imagine we are programming a system and need to process something. For example, a function that receives a duck 🦆 comes to mind.
- In Static Typing (C++, classic C#), we are like customs control. We ask for a passport:
Are you a Duck? Show me your accreditation of type
Duckor anIDuckcard. If not, you don’t pass
- In Duck Typing (Python, JS), we are more pragmatic. We don’t ask for identification. We simply try to interact:
Can you quack? If you can, pass. I really don’t care if you’re a duck, a person in disguise, or a recorder
In summary, in Duck Typing we care about what the object can do, not what the object is.
Deciding whether Duck Typing is a “typing” concept stretched thin, if it’s not typing at all, or is typing with a different philosophy and mindset, is up to you (…it’s probably all at once)
- Extreme flexibility: You can create new classes that work with old code without needing to modify complex inheritance hierarchies.
- Less code (Boilerplate): You don’t need to write huge interfaces or constant casts.
- Implicit polymorphism: Things just work together if they “speak the same language”.
- Runtime errors: If you pass an object that does not have the expected method, the program will crash while running (reaches the user), not while compiling.
- Worse IDE help: The code editor sometimes doesn’t know what methods to suggest because it doesn’t know what type of object will actually arrive.
- More difficult refactoring: It’s harder to find all the places where a method is used if there isn’t an explicit type linking them.
A Practical Example in Python
Python is the king of Duck Typing. Let’s see a very clear example. Imagine we have a function that makes things “fly”.
class Pato:
def volar(self):
print("El pato aletea y vuela")
class Avion:
def volar(self):
print("El avión enciende motores y despega")
class Ballena:
def nadar(self):
print("La ballena nada")
def hacer_volar(cosa):
# Here's the magic: We don't check the type.
# We just try to execute the .volar() method
cosa.volar()
pato = Pato()
avion = Avion()
ballena = Ballena()
hacer_volar(pato) # Works: El pato aletea...
hacer_volar(avion) # Works: El avión enciende...
hacer_volar(ballena) # ERROR: AttributeError: 'Ballena' object has no attribute 'volar'
Notice what happened. Pato and Avion have no relationship between them. They don’t inherit from a common Volador class. They don’t implement a common interface.
However, the hacer_volar function accepts both. Why? Because both have a volar method. They walk like a duck.
The Ballena, lacking that method, fails at runtime.
Duck Typing in JavaScript
JavaScript works the same way. Being a prototype-based and dynamic language, we can pass any object to a function.
function imprimirLongitud(elemento) {
// Duck typing: We assume 'elemento' has a 'length' property
console.log(elemento.length);
}
imprimirLongitud("Hola"); // 4 (Strings have .length)
imprimirLongitud([1, 2, 3]); // 3 (Arrays have .length)
imprimirLongitud({ length: 10 }); // 10 (Any object with a .length property)
imprimirLongitud(50); // undefined (Numbers don't have .length)
Here we see the potential of the concept. The imprimirLongitud function works with strings, with arrays. And even with an object we invented, as long as it has the length property.
And in Static Languages like C#?
Traditionally, in C# or Java, to achieve this we would need to use Interfaces.
// In strict C# (Traditional way)
interface IVolador {
void Volar();
}
public void HacerVolar(IVolador cosa) { ... }
This is not Duck Typing. This is polymorphism by contracts. Here we force classes to sign a contract (IVolador).
To do something “truly similar” to Duck Typing in static languages, there are some solutions.
But, in general, Duck Typing is a natural path in Python/JS, but a “special guest” (and not very well received) in languages like C++, Java, or C#.
