Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Overriding/overloading.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I don' t understand why the following code prints "Scale Shape". This example is about overloading (not ovveriding) and I supposed that it prints "Scale Circle"..



tks
bye
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here the scale method is not overriden instead overloaded.

So overloaded method is resolved at compile time and hence it does not depends on the object at runtime.

Compiler resolve the method at runtime only if the method is overriden.

So the compiler will call the method based on reference.
 
Tony King
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks!

I did not know that compiler resolve the method at runtime ONLY if the method is overriden.

Bye
 
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

Siva Masilamani wrote:
Compiler resolve the method at runtime only if the method is overriden.



No, Compiler is not at all related to the runtime.[This is Overloading . not an overridding]so, in Shape class there is no scale(int) mothod. so compiler complaint.

is it make sense?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok

i have to change my Word like this because compile has nothing to do at run time.

so

Compiler resolve the method at runtime only if the method is overriden



should be something like this.

the determination of which overridden method to execute isn't made until runtime
 
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

Siva Masilamani wrote:the determination of which overridden method to execute isn't made until runtime



Once again, i tell you.it is not an Overriding.still you need to change your sentence
 
Tony King
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm confused..
Can you help me???
 
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

Tony King wrote:Now I'm confused..



why you confused ?
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what ever it may be, i have learned a new point today.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

seetharaman venkatasamy wrote:

Siva Masilamani wrote:the determination of which overridden method to execute isn't made until runtime



Once again, i tell you.it is not an Overriding.still you need to change your sentence



That sentence is not particular for his program.

It is in general that overriden method will be resolved at runtime and overloaded method will be resolved by the compiler,that is compiler knows which overloaded method to be called.

So in his program it is overloaded and hence the method call is based on reference not on actual object

To put it in simple words.

If method is overloaded check the reference and if the method is present then no error and the method will be called based on reference.

If the method is overriden check the reference and if the method is present on that reference then no error but the method call will be based on object at runtime not on reference type.

The above conditions hold good only for instance method not for class method.

Also any variable(class or instance) always refered with reference type not on object type.
Hope it clears everyone confusion
 
Tony King
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, now the concept is clear. Thank you, the answer is exhaustive!

bye
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic