Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i dont understand this polymorphisim 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 "i dont understand this polymorphisim" Watch "i dont understand this polymorphisim" New topic

i dont understand this polymorphisim

Caglar Cataloglu

Joined: Feb 06, 2010
Posts: 25

When i run this code, the result is:

PlayerPiece displaying shape.

But how can i reach PlayerPiece method without using casts? I dont get it.

Java Lover
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
it depends on object type at runtime that which method will be called.
you are passing an object of type PlayerPiece to the doShape method which takes GameShape as arguments and this is legal because Playerpiece extends GameShape.

Now at runtime jvm sees that object referred by shape variable is actually a PlayerPiece object and it will call the overridden method.

SCJP 1.6 96%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Here, the actual object is PlayerPiece. For method doShapes(), we upcast that. But for at runtime, the method is invoked on the actual object. Not in the reference type. This is called as Virtual Method Invocation!

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

If you always want to call the super class method before the child class method be executed you can use super key word in sub class method. But you can't directly call super class (which is overridden in subclass) method by using child class reference.
Caglar Cataloglu

Joined: Feb 06, 2010
Posts: 25

Thank you for your answers.

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You are welcome
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
This is polymorphism in action. You passed in a PlayerPiece object and it was accepted
by doShapes() because PlayerPiece is indeed also a GameShape. But the object passed
remains a PlayerPiece. So PlayerPiece.displayShape() is called, not the GamePiece
version. It's the type of the object, not the type of the variable (local in this case) that
determines the procedure version called.

This may help. What if GameShape were an interface rather than a concrete class.
You could cast a PlayerPiece into GameShape (to focus on GameShape behaviors) but
a GameShape object could not be created. PlayerPiece.displayShape() would be the
only option available.


Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
Polymorphism is explained rather amusingly in this introductory article in Java Ranch.

Rajeev Trikha (SCJP 6)
Edward Kin

Joined: Dec 09, 2008
Posts: 18
I tend to find it easier to think of more than one class extending the super class. If you had PlayerPiece and ChessPiece and DraughtsPiece as classes all extending GameShape then it becomes clearer to my mind how the polymorphism works.

The doShapes() method can take any object as the argument as long as it is a subclass of the GameShape object.

Additionally the doShapes method will continue to work when/if you introduce new GameShape subclass objects.

p185 of Head First Java : "...with polymorphism, the reference and the object can be different"

There is a clear example on p187 involving Animals and Vets and Dogs/Hippos (subclasses of Animal).

In your case the shape.displayShape(); [line 13] will refer to the actual object on the heap (late binding) and the corresponding subclass method will be called rather than the superclass method.

I'm still learning but this is my understanding - (might need correcting!).
I agree. Here's the link:
subject: i dont understand this polymorphisim
jQuery in Action, 3rd edition