Jump to content

Please explain Polymorphism

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Patrick

Patrick

    Programmer

  • Members
  • PipPipPipPip
  • 101 posts
Can anyone please explain me what is Polymorphism giving any small example. I don't need any program as example , any simple idea from real world will also help or mention any helpbook if you have any idea of

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A cat is an animal. A dog is an animal. A dog is not a cat and vice versa.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
To understand polymorphism, you need to understand a little bit about class inheritance and hierarchy.


      ANIMAL

     /       \

   DOG       CAT

    |        || |

Rottweiler   ||  \ Cheshire

             | \ Tabby

             \ Tiger


Hierarchy:
Anything defined in a level above the current level can be accessed by lower levels. A Rottweiler is a dog, but a dog isn't necessarily a Rottweiler.

Inheritance:
Cheshires, Tabbies, and Tigers are on the same level; they can each access the functions and fields specified by CAT, but each has its own functions and fields that other cats can't access. For example, tigers can't execute Cheshire.hugeSmile(), but both Cheshires and tigers can execute CAT.cleanSelf().

The polymorphism part:
Every animal has a saySomething() method, but it will do something different for each subclass of animal. Tabby.saySomething() will give "Meow", but Cheshire.saySomething() will give ":D".

#4
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
Also check here: http://forum.codecal...ht=Polymorphism