Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

mixed variable overloading  RSS feed

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Report post to moderator


y1 is a mixed varible.
to what fuction should i put y1
a function f that inputs A variable or B variable
?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
First, lets add some formatting, as unformatted code is difficult to read...

alex lotel wrote:



alex lotel wrote:
y1 is a mixed varible.
to what fuction should i put y1
a function f that inputs A variable or B variable
?


The overloaded signature to call is determined at compile time, via its reference. And at run time, the overridden method is called based on the actual object type. In your example, at compile time, z1 has the signatures of an A class, so the compiler only knows about the f() method that takes an A instance. And since it refers to a C instance, at run time, it will call the method that takes the A instance, that is declared in the C class.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator

Also, noticed that this topic is a duplicate of this one....

http://www.coderanch.com/t/590223/java/java/choosing-overloading-method


Please don't post duplicate topics, as it wastes people's time and effort.... and it this case, I guess it wasted my time and effort...

Henry
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!