• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt in Overloading / polymorphism.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
Please look at the given codes. Can anyone explain why the output is different? I know it has something to do with dynamic/ static binding. But can anyone please explain a bit. Doesn't it violates the core polymorphism principle? I thought Java is a OO language !




output:
A:class D
D:class D

Regards,
Sajid.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel ok with the answer.

d1 is of type A but holding the reference of D ( Legal : D is subclass of A)



d2 is of type D and holding the reference of D

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


When the above code is encountered, since it is over loading, it takes care of finding out the right methods based on the method signature and type.

when you do



That's where polymorphism comes into picture. Based on the object that is passed, in your case, you are passing object of type D in both the cases. Hence getting the same class name.

Hope it helps.
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which kind of method will be invoked is determined at compile-time based on parameters provided (overloading) - so at compile time you have signature of method that will be called. If there are more methods with same signature in class hierarchy (overriding) then which one of them will be called is determined at run-time based on actual type of instance.



gives:
 
and POOF! You're gone! But look, this tiny ad is still here:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic