-5

I have been working on oop on different programming language, I have used different manuals and i realized that practically all of them explained polymorphism differently, although I am fine with all their explanation in terms of getting to use it but can anyone just explain this concept in a lay man general term and concept with practical example?

Thanks

Reply appreciated

Temitayo
  • 802
  • 2
  • 12
  • 28
  • Polymorphism in general refers to the ability of a component/method/object to behave differently under different specific conditions/situations. – Xavi López Feb 17 '14 at 11:51
  • @XaviLópez can u please give a practical example – Temitayo Feb 17 '14 at 11:53
  • There are lots out there. For instance, this is a classical one: an `Animal` can `makeSound()`. A subclass of `Animal` called `Dog` will bark when implementing `makeSound()` and another one named `Cat` will meow. The `makeSound()` method behaves differently for an `Animal` depending on which is the actual subclass of the instance. – Xavi López Feb 17 '14 at 11:54
  • Thank, but I wonder why my question was voted down, it is just a question that needs understanding........... – Temitayo Feb 17 '14 at 11:59
  • It might have been voted down because of the numerous existing duplicates of this question. You can see lots of them in the right side of this page under the _Related_ questions section, and they should have appeared as suggestions when you were writing your question. Take a look at [How do I ask a good question](http://stackoverflow.com/help/how-to-ask). – Xavi López Feb 17 '14 at 12:09

1 Answers1

2

Here is a direct copy paste from wikipedia explaining xavi Lopez example:

"There are lots out there. For instance, this is a classical one: an Animal can makeSound(). A subclass of Animal called Dog will bark when implementing makeSound() and another one named Cat will meow. The makeSound() method behaves differently for an Animal depending on which is the actual subclass of the instance. – Xavi López"

abstract class Animal {
    abstract String talk();
}

class Cat extends Animal {
    String talk() { return "Meow!"; }
}

class Dog extends Animal {
    String talk() { return "Woof!"; }
}

void lets_hear(Animal a) {
    println(a.talk());
}

void main() {
    lets_hear(new Cat());
    lets_hear(new Dog());
}

As you can see, the animal has sever different extensions. So depending on animal type, a different thing will be printed.

The output looks like:

>>Meow!
>>Woof!
TheOneWhoPrograms
  • 629
  • 1
  • 8
  • 19