Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism code help

 
joe bob
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch joe .
 
Mala Gupta
Author
Ranch Hand
Posts: 291
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

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



With respect,
Mala
 
joe bob
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it now, thanks yo all guys.
 
Mala Gupta
Author
Ranch Hand
Posts: 291
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1229
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope the following sample code helps you to understand it better:



Output of the above code:



With respect,
Mala
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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! ^_^

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic