aspose file tools*
The moose likes Beginning Java and the fly likes Polymorphism doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism doubt" Watch "Polymorphism doubt" New topic
Author

Polymorphism doubt

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055



when i say a.getSal("test") the output should be Enginner Setting sal... but the output is Employee getting sal... why so?


http://www.lifesbizzare.blogspot.com || OCJP:81%
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Add the @Override annotation to Engineer.getSal and you'll see that Mohamed is right.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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
Greenhorn

Joined: Apr 08, 2010
Posts: 28

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...


"It's good to B in touch...So keep responding"
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism doubt