This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding Clarification" Watch "Overriding Clarification" New topic
Author

Overriding Clarification

James Hoare
Greenhorn

Joined: Aug 11, 2006
Posts: 9
Hi Ranchers, could someone please explain why at runtime the method in Foo is called rather than the subclass' method? The code below will print 1, I understand why it doesn't invoke the var-args method but why does it not match on the primitive long subclass method at runtime?

Thanks in advance.

class Foo {

int doFoo(int x) {
return 1;
}
}

class SubFoo extends Foo {

int doFoo(int... y) {
return 2;
}

int doFoo(long z) {
return 3;
}

public static void main(String... args) {
Foo f = new SubFoo();
System.out.println(f.doFoo(5));
}

}
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi James,

Welcome to JavaRanch,




What you talk about is the case when the subclass would have overridden the Foo methods. subclass in your code does not override the method doFoo(int i) of the Foo class; the subclass overloads the method doFoo(...), hence the
Foo(int x) version inherited by the subclass is called at run time because JVM does not find overridden method by the subclass.

Anyways, we know var-arg as looser;
Widening beats boxing.
Widening beats var-arg;

What you pass to the method is primitive so int version of the method is called.
[ April 30, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Because the method that accepts an int parameter is inherited and is more specific than the one that accepts a long.
James Hoare
Greenhorn

Joined: Aug 11, 2006
Posts: 9
Thanks so much, it's easy when you know how! Bit of a schoolboy error on my part.

Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
just to add to whats been said:

when you write int doFoo(int... y) {return 2;}

the code generated will be int doFoo(int[] y) {return 2;}

so its an overload not an override.

do a javap classname to see this for yourself


Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Louis,

Really! Can you focus some light on it please..


Thanks,
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
hi Chandra,

if you type in the code compile it, then do a:

javap SubFoo

this will show you the signatures of the methods of the class that have been generated.


i presume when you do a doFoo(8) on SubFoo it pass a 1 element array.


you can also do a:

javap -c SubFoo

to see the desembled class code
[ April 30, 2007: Message edited by: Louis Moloney ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Louis,

But I don't think, this has anything to do with James question.
In his question, it was just matter of choosing more specific method where int
can be passed. There is no method overriding by the subclass.

I understand,
var-arg is converted to corresponding array by the compiler and
var-arg can't be overridden with array argument.

Doubt:
What in case when you dont pass any argument to the method that asks
var-arg. It accepts. If it is converted to array argument it should not
be called on blank argument.

I tested the same with array argument method, and got compiler error.

Is that correct?
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
Hi Chandra,


Why does it compile and return 2?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by Surasak Leenapongpanit:
Hi Chandra,


Why does it compile and return 2?



Hi,

When I compile the Test.java, the result is following:




I used javap to see what happened behind:
For A.class


For B.class:



Findings:
You should not (I wont say can't because code compiles with warning ) override a method that is with varagrs parameter with array and vice versa is also true. It gives warning.

Actually all the var-args are converted with array parameter. Compiler does so, that is why the code compiles fine. Overridden version of the method is
called polymorphically (run time decision).
Ram Gopal
Greenhorn

Joined: Apr 06, 2007
Posts: 14
Hi i have one doubt abt overriding concept. As you know, "When a method in subclass has same name and type signature as a method in superclass, then the subclass method is said to override superclass method."

well, what does "type signature" mean?
 
 
subject: Overriding Clarification
 
Similar Threads
Generic Method - Shouldn't this be correct?
Classloader quizzer
Overloaded method
Class Hierarchy nightmares
why this output?