Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in overloading

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


now here at compile time wont the parent string be selected based on the Parent reference? why is the output then "child"
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, this example is not overloading, it's overridding...

Ankur kothari wrote:
now here at compile time wont the parent string be selected based on the Parent reference? why is the output then "child"


Yes, the string is referenced at compile time. So, the Parent class' say() method will reference the Parent string. And the Child class' say() method will reference the Child string... both determined at compile time.

In your main method, you are not referencing any string. You are calling the say() method, which is resolved at runtime.

Henry
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.....got you....i have one more doubt

what is Number here in get() method...what kind of return is this? still the program compiles

 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:
what is Number here in get() method...what kind of return is this?


java.lang.Number

Henry
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Number is super type of Integer,Sort.....
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having Integer and Number as return type is an example of covariant return.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic