• Post Reply Bookmark Topic Watch Topic
  • New Topic

varars parameter in method changes the polymorphism ?  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


OUTPUT :

Alpha : foo
Beta : foo
Beta : bar
Beta : bar

I am confused as normally the derived method is supposed to run when I call a.foo("") but in this case the method with varargs executes !
Could you guys explain me please (:
Thanks in advance
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you are not overriding the foo method as you don't have exactly same signature for Beta class.
If you keep exactly same signature then you would get expected result.

Manish
 
Raja gopal Mani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets take this code for example


I tried to apply vararg parameter and String parameter in different combinations to the Parent and Derived Class methods. I am slightly confused with the results I obtained

PARENT Class Method - VARARG & DERIVED Class Method - STRING
Alpha : foo
Beta : foo

PARENT Class Method - STRING & DERIVED Class Method - VARARG
Alpha : foo
Alpha : foo

PARENT Class Method & DERIVED Class Method - VARARG
Beta : foo
Beta : foo

I seriously feel something different in the results obtained !! Not sure whats happening :|
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raja gopal Mani wrote:Lets take this code for example


I tried to apply vararg parameter and String parameter in different combinations to the Parent and Derived Class methods. I am slightly confused with the results I obtained

PARENT Class Method - VARARG & DERIVED Class Method - STRING
Alpha : foo
Beta : foo

PARENT Class Method - STRING & DERIVED Class Method - VARARG
Alpha : foo
Alpha : foo

PARENT Class Method & DERIVED Class Method - VARARG
Beta : foo
Beta : foo

I seriously feel something different in the results obtained !! Not sure whats happening :|


Bottomline is that if derived class's method is not exactly same in terms of parameters then its not
method overriding...its method overloading.Overloaded methods are resolved on basis of reference type
while overridden method calls are resolved during runtime.

Alpha a = new Beta();
Beta b = (Beta)a;
a.foo("Test"); b.foo("Test");

PARENT Class Method - VARARG & DERIVED Class Method - STRING
Now when you call foo() on a then as a is of parent type so call goes to parent class version, hence output is Alpha:foo.
When foo() is called on b then as there are two versions of foo() available, so one with exact match is called, hence Beta:foo.

PARENT Class Method - STRING & DERIVED Class Method - VARARG
Same logic here, so a.foo() prints alpha:foo while for b.foo() exact match is the version which is in parent, hence output is alpha:foo.

PARENT Class Method & DERIVED Class Method - VARARG
Here method overriding happens, hence method call is resolved during runtime and that is a call to parent
version in both cases.


 
Raja gopal Mani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for the clarification !
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Another option -- if you believe that a method is overriding it's superclass method, then use the @Override annotation. If the compiler disagrees with you, it will complain. No need to guess if a method is overriding or not, just let the compiler confirm it for you.

Henry
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Another option -- if you believe that a method is overriding it's superclass method, then use the @Override annotation. If the compiler disagrees with you, it will complain. No need to guess if a method is overriding or not, just let the compiler confirm it for you.

Henry


But for the beginners this may impede the understanding of basic java concepts.....just my thought

Manish
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!