-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

5
  • Polymorphism in general refers to the ability of a component/method/object to behave differently under different specific conditions/situations. Commented Feb 17, 2014 at 11:51
  • @XaviLópez can u please give a practical example Commented Feb 17, 2014 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. Commented Feb 17, 2014 at 11:54
  • Thank, but I wonder why my question was voted down, it is just a question that needs understanding........... Commented Feb 17, 2014 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. Commented Feb 17, 2014 at 12:09

1 Answer 1

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! 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, expatiated answer no critics, God bless u
Im glad it helped. If its all that you wanted, please mark it as the "Accepted Answer". If not, please explain what you would like me to expand, and then I can modify my answer for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.