File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding Clarification

 
James Hoare
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the method that accepts an int parameter is inherited and is more specific than the one that accepts a long.
 
James Hoare
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much, it's easy when you know how! Bit of a schoolboy error on my part.

 
Louis Moloney
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Louis,

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


Thanks,
 
Louis Moloney
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,


Why does it compile and return 2?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic