What is polymorphism in Java?
Polymorphism is the ability of a program to identically use objects with the same interface without knowing the specific type of that object. As they say, one interface, many implementations. With the help of polymorphism, you can combine and use different types of objects according to their common behavior. For example, we have the Animal class, which has two heirs – Dog and Cat. The general Animal class has a common behavior for everyone – make a sound. In the case when you need to collect together all the descendants of the Animal class and execute the “make a sound” method, we use the possibilities of polymorphism. Here’s what it will look like:
List<Animal>< /span> animals = Arrays.asList(new Cat(), new Dog(), new Cat ()); span>
< /pre>
animals.forEach(animal -> animal.makeSound(< /span>));
So polymorphism helps us. And this also applies to polymorphic (overloaded) methods.