File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism code help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphism code help" Watch "Polymorphism code help" New topic
Author

Polymorphism code help

joe bob
Greenhorn

Joined: Feb 13, 2013
Posts: 5
Hi guys,

I got a problem to figure out the following code the compiler output is MarkHenry55555555, but I thought the actual reference type would be called in the runtime which is a reference of type publication class to give this output "HenryHenry11111111". What I missing here, thanks in advance.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
If a method is static then the type of the reference variable will decide what method is called.
If a method is not static then the type of the object the reference is referring to will decide which method is called.

Does that answer your question ?


Joanne
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch joe .
Mala Gupta
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 245
    
    9
Joe,

I have changed your static methods to instance variables. Can you determine the output of the following code?



With respect,
Mala


Author of Manning's OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
joe bob
Greenhorn

Joined: Feb 13, 2013
Posts: 5
I got it now, thanks yo all guys.
Mala Gupta
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 245
    
    9
Joe,

Don't get me wrong! Can you please explain the output of the modified code - where static methods were replaced with instance variables.

With respect,
Mala
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

I am still not understanding it, besides I ran the code several times

Isn't supposed when comes to static methods , polymorphism doesn't apply? Isn't exactly the first line Joanne Neal says ? => a method is static then the type of the reference variable will decide what method is called.

Then why the output looks like if polymorphism is applied :S

(Edited due grammar )
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 594
Hi David Samer,
Polymorphism does not apply to static instance.
Static methods cannot be overriden, but they can be redefined.
In this example,

s is an instance of Subject. Since getName() is a static instance in both classes, s.getName() is equivalent to : Subject.getName().
Mala Gupta
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 245
    
    9
Hope the following sample code helps you to understand it better:



Output of the above code:



With respect,
Mala
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Greetings there Himai Minh ;) and Mala Gupta.

Both have given me the key to understand it, specially your example Mala . Thank you so much , crystal clear finally! ^_^

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism code help
 
Similar Threads
Determine type of a primitive
Not able to get mapped object Set in hibernate
passing Obj reference to the method
Anyhting like super.super?
SCJP related .....