In java we can have polymorphism. Super type can be made to refer to sub type. E.g Animal referring to Dog. But whether Animal refers to Dog or Dog refers to Dog ,at runtime the method called will be of Dog type only.So the what was the benfit which we got here because of Polymorphism. One advantage is readibility.Is it the only advantage or there is some other advantage too.
Monica. Shiralkar wrote:One advantage is readibility.Is it the only advantage or there is some other advantage too.
There are lots of advantages, the main of which is to be able to deal with multiple similar types automatically, without the need for "dispatch logic" - ie:Indeed, if you see a Java program with lots of code like that, it often means that the designer didn't use polymorphism when they probably should have.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
There are lots of advantages, as Winston points out, but if you want to get really technical, they all boil down to easier for programers to write and easier for programers to read. In fact, all computer languages, from assembly on up, are exclusively meant to simplify the interface of binary computers to human programers. The computer would be just as happy if we wrote everything in machine language, but we wouldn't be.