• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt Regarding Polymorphism

 
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I am having a doubt regardig polymorphism. I have a base class Animal.child class Dog.

Ex:-Animal a = new Dog();

What this statement tells?In real time where this situation will occur...

Thanks in Advance,
Vipul.
 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are creating the dog object and giving it to a animal reference.
Such situation might occur when you create List Object.
List is a interface , so you create a Object for arrayList or any of the classes that implements List and referenc it to List .

List l = new ArrayList();
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashwin,

This answer iam expecting. Actually my doubt starts here.we can directly write
ArrayList al = new ArrayList();
instead of
List l = new ArrayList();
What is the use in instantiating ArrayList and giving reference to List.?

Thanks in Advance,
Vipul.


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typically, It is better to program with super class/interface reference, this avoid the a class specific implementation method call in your code. for example,
pretend you love Dog now,

after some time you decided to go for a cat since you dont like the Dog

if you could have been used like this

in(1), you may tempted to use Dog class specific method, later when you change Dog to Cat
you are required to change the line (1) also , so totally two lines are modified, so maintenance may be nightmare there!

this is a small example with two lines, think about thousands of line!


 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman ,

Is this the only reason or any other logical reason. When i see that kind of statements iam confused a little bit.

and this is called as Run-time polymorophism,right?

can anyone tell me a real time example of this run-time polymorphism.

Thanks in Advance,
Vipul
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at this hot thread!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, Please SearchFirst
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman,

I got my answer here.

"Polymorphism is defined as one interface to control access to a general class of actions."

This answer supports what you have said...Thank you for your suggestions..

Thank you.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vipul bondugula wrote:Thanks Seetharaman


you are welcome
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vipul bondugula wrote:
"Polymorphism is defined as one interface to control access to a general class of actions."


Sorry for the late reply! I suggest you to read this
reply
    Bookmark Topic Watch Topic
  • New Topic