• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism doubt

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when i say a.getSal("test") the output should be Enginner Setting sal... but the output is Employee getting sal... why so?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you overriding the getSal() method? From what it looks- its overloading the method. So the output which you are expecting comes into play- only when the method is overridden.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the @Override annotation to Engineer.getSal and you'll see that Mohamed is right.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohamed sanaullah wrote:Are you overriding the getSal() method? From what it looks- its overloading the method. So the output which you are expecting comes into play- only when the method is overridden.


Ok, correct its overloaded.But When overloading using var-args and primitive

and calling a.getsal("test") //then primitive method should be called??? let me know if i am wrong?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:
Ok, correct its overloaded.But When overloading using var-args and primitive
and calling a.getsal("test") //then primitive method should be called??? let me know if i am wrong?


You're correct that var-arg methods will be given lower priority. But in this case the reference type is Employee, which doesn't have the getSal(String) verson of the method. The polymorphism doesn't apply when overloading, only when overriding.

(Oh, note that it's not "primitive" - String is a class).
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Vishal Hegde wrote:
Ok, correct its overloaded.But When overloading using var-args and primitive
and calling a.getsal("test") //then primitive method should be called??? let me know if i am wrong?


You're correct that var-arg methods will be given lower priority. But in this case the reference type is Employee, which doesn't have the getSal(String) verson of the method. The polymorphism doesn't apply when overloading, only when overriding.

(Oh, note that it's not "primitive" - String is a class).


Both the Employee and Engineer a has getSal(String) version of method

For Employee its
public void getSal(String...args)

For Engineer its
public void getSal(String a)

My question is why a.getSal("test") is calling Var-arg method and not the method in the Engineer class
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the reference type is Employee, so the compiler doesn't know there is a getSal(String) - it only knows about getSal(String...).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:My question is why a.getSal("test") is calling Var-arg method and not the method in the Engineer class


Because, as I said, the reference type is Employee. getSal(String) does not exist in the Employee type - rather, there is a method (the var-arg one) that can be called with a single String as argument. But that's not the same thing.

Basically, it works like this. The compiler will use the reference type to decide which method (name and signature) to call. Then, at run-time, if the method has been overridden the JVM will decide the version of the method to call based on the actual type of the object. But that only applies if the method in the sub-class has exactly the same signature.

In your case the signature is different. So it's overloading, not overriding, and the JVM treats it as if it's a completely different method.
 
Mohit J Kumar
Ranch Hand
Posts: 33
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Employee getting sal... is a perfectly valid output.
What you are doing here is method overloading not overriding.
In case of overloading.....the method to be called is decided by the calling reference and
not the actual object being referenced by the reference.
Here Reference is of type Employee & referred object is of Engineer class.
In case of overriding you will get
Engineer getting sal...

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This related thread must be of some help- http://www.coderanch.com/t/520502/java-programmer-SCJP/certification/polymorphism

Update: Coincidentally both of them have the same subject.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic