Sobes.tech
Junior — Middle

Can you explain the concept of polymorphism in object-oriented programming?

sobes.tech AI

Answer from AI

Polymorphism is a principle of object-oriented programming that allows objects of different classes to have the same interface (set of methods) and behave differently when these methods are called.

In simpler terms, the same method can work with objects of different types, invoking their respective implementations.

For example, classes Cat and Dog have a method make_sound(). For a cat, it might output "meow", and for a dog — "woof". The code calling make_sound() does not care which class the object belongs to — it simply calls the method, and the behavior depends on the specific object.