• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

polymorphism with var args

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys ... i had some personal problem for which i couldnt respond back for yesterdays thread...
HERE is the actual correct code which i wanted to share with you guys..

The output is "its A"
its surprising output is not "its Troy"..
no polymorphism here.... can anyone tell why???


 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rajiv Nair.. that was one more exploration for me.. check this out .. its concerned to polymorphism call to a method..
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Shaid,

The output should be "its A" and it is.

At this statement t.foo("test"); the behaviour of compiler and JVM as below:


compile time:

At compile time the compiler checks whether the parent class has any method which is compatible with single argument String as it is allows you to compile.

runtime :
At runtime as the parent class method is not overridden, child object has both methods(from parent as well as from child). So JVM calls child class method only in case when the parent class method is overridden.In this case it is not overridden because
foo(String... a) and foo(String a) are not same.


 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This example of method overloading. We can achieve compile time polymorphism through overloading method, on compile time compiler decide that which method
will be executed.

If you override method instead of overloading like below then it will produce output as - its Troy
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks navin.. that was helpfull and thanks pramod... the sub class has both the methods as its not overriding over here... cool..
 
Ranch Hand
Posts: 37
Netbeans IDE Spring Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohammad shaid wrote:Thanks navin.. that was helpfull and thanks pramod... the sub class has both the methods as its not overriding over here... cool..


this is about var-args,

In the Animal constructor, is the Array syntax for the var-args ok ?
How to assign the var-args ?
 
Chanakya Gupta
Ranch Hand
Posts: 37
Netbeans IDE Spring Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys I passed the SCJP6 today with 71%. Thanks a lot for
the excellent posts. It helped a lot.
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


class A {
public void foo(String... a) {
System.out.print("its A");
}
}
class Drag extends A{
public void foo(String a) {
System.out.print("its Troy");
}
public static void main(String[] args) {
A t=new Drag();
t.foo("test");
}
}




your code is neither overloading nor overriding the foo()method
but i think it is simply method calling.

This is what Nevin Kumar wants to say about your example


Nivin Kumar wrote:
compile time:
At compile time the compiler checks whether the parent class has any method which is compatible with single argument String as it is allows you to compile.

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chanakya Gupta wrote:

mohammad shaid wrote:Thanks navin.. that was helpfull and thanks pramod... the sub class has both the methods as its not overriding over here... cool..


this is about var-args,

In the Animal constructor, is the Array syntax for the var-args ok ?
How to assign the var-args ?



can someone please explain this..
as far as i can understand.
first of all a object of type DOG is created with parameters "tommy" and 11
which leads to calling of constructor of DOG class which first calls the constructor of ANIMAL class.
but since the constructor of ANIMAL class has only an int...x as argument type..what happens next ??

thanks
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chanakya Gupta wrote:

mohammad shaid wrote:Thanks navin.. that was helpfull and thanks pramod... the sub class has both the methods as its not overriding over here... cool..


this is about var-args,

In the Animal constructor, is the Array syntax for the var-args ok ?
How to assign the var-args ?



When you creating the Dog object, you've called the Dog's constructor with the new operator. But in the Dog's constructor, compiler added super() as the first line of the Dog's constructor. So that[super()] invokes the super class constructor(in your case, Animal class), Since you have variable argument constructor in your Animal class constructor, it invokes with zero arguments(no arguments). But inside the Animal's constructor, you access the 0th and 1st index of the array. But that array is null, because you passed zero arguments. So ArrayIndexOutOfBoundsException will be thrown!

Note : variable argument will take zero to many arguments. In your case, you've passed zero arguments.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chanakya Gupta wrote:
In the Animal constructor, is the Array syntax for the var-args ok ?
How to assign the var-args ?



Your array syntax is correct, but the logic is wrong. You can assign that array in to another array or getting the length of the array, and according to that, you can assign the values further. Otherwise,these kind of IndexOutOfBoundsExceptions will be thrown.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Srinivasan wrote:
can someone please explain this..
.
.

but since the constructor of ANIMAL class has only an int...x as argument type..what happens next ??
thanks



Check my first post in this thread!
 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks :) :thumbup:
 
Chanakya Gupta
Ranch Hand
Posts: 37
Netbeans IDE Spring Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Srinivasan wrote:thanks :) :thumbup:


Thanks Abimaran...that was helpful, thanks Srinivasan and sumit.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chanakya Gupta wrote:
Thanks Abimaran...that was helpful,



Welcome! :wink:
 
I once met a man from Nantucket. He had a tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic