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
Please explain Polymorphism
Started by Patrick, Oct 06 2007 10:53 PM
3 replies to this topic
#1
Posted 06 October 2007 - 10:53 PM
|
|
|
#2
Posted 08 October 2007 - 08:30 AM
A cat is an animal. A dog is an animal. A dog is not a cat and vice versa.
#3
Posted 08 October 2007 - 02:58 PM
To understand polymorphism, you need to understand a little bit about class inheritance and hierarchy.
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".
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_*
Posted 09 October 2007 - 05:09 PM
Guest_NeedHelp_*
Also check here: http://forum.codecal...ht=Polymorphism


Sign In
Create Account


Back to top









