aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism problem 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 problem" Watch "Polymorphism problem" New topic
Author

Polymorphism problem

Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115



Is this correct program of polymorphism ?

and i am looking for more alternative ways of these declarations so i can easily understand this.

dhiru kumar
Greenhorn

Joined: Jul 30, 2011
Posts: 21
Hi Nitesh ,
yes it is a correct Polymorphism , but I don't think its serving the purpose of it.
Because anyways at runtime it will call the methods of GameShape only.
TestPiece and GamePiece doesn't override the method " displayingShape() ", they inherit it from the super class.

I can be wrong .
Lets wait for others to post.
--Dhiru
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

dhiru kumar wrote:Hi Nitesh ,
yes it is a correct Polymorphism , but I don't think its serving the purpose of it.
Because anyways at runtime it will call the methods of GameShape only.
TestPiece and GamePiece doesn't override the method " displayingShape() ", they inherit it from the super class.

I can be wrong .
Lets wait for others to post.
--Dhiru


I am also confused about this example of polymorphism but it is given in book K&B 6.0 chapter2 page no.93 and there it is explained that even with subclass obj you can call only superclass method not of subclass. is it called polymorphism ?
ket bhav
Greenhorn

Joined: Aug 28, 2011
Posts: 22
Hi there,

just try

and try again to run.

And continue thread with your opinion.

Thanks.
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

ket bhav wrote:Hi there,

just try

and try again to run.

And continue thread with your opinion.

Thanks.


as i've already mentioned above code wont work.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Polymorphism means that a subclass can override a method defined in the superclass and specify its own behavior.
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Dennis Deems wrote:Polymorphism means that a subclass can override a method defined in the superclass and specify its own behavior.


you didn't explain whole thing, it is not complete because real magic start from main() where we play with objects .
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
...
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Dennis Deems wrote:...


Dennis don't worry after learning it i will post here in detail so everyone can learn it because noone have dare to explain it.
Eli Wood
Ranch Hand

Joined: Sep 04, 2008
Posts: 37

Polymorphism in Java is simply the ability to access an object through a reference to that object's class, a reference to a superclass, or a reference to an implemented interface. It may help to remember that poly- means many and morph- means form. The object can seem to have many different forms depending on the kind of reference used to access it.

Since, for example, the program you provided uses a GameShape reference to point to a PlayerPiece object, I would say that it provides a simple example of polymorphism.

Other ways you could declare your objects?

Output of main() function:
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
See my comments below, and I let the rest for you to understand why...hope it help.




OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Eli Wood
Ranch Hand

Joined: Sep 04, 2008
Posts: 37

Tommy, if no overriding function is provided for a parent class's public instance methods then the subclass will simply inherit them, so the following two comments are incorrect. You can see the output I have appended to my earlier post to see those methods will execute.
Tommy Delson wrote:
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Eli Wood wrote:Tommy, if no overriding function is provided for a parent class's public instance methods then the subclass will simply inherit them, so the following two comments are incorrect. You can see the output I have appended to my earlier post to see those methods will execute.
Tommy Delson wrote:



When testing with the package name it won't works, but without package name it works. I didn't make it clearly or changed on the comments based on two tests.
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Tommy Delson wrote:
Eli Wood wrote:Tommy, if no overriding function is provided for a parent class's public instance methods then the subclass will simply inherit them, so the following two comments are incorrect. You can see the output I have appended to my earlier post to see those methods will execute.
Tommy Delson wrote:



When testing with the package name it won't works, but without package name it works. I didn't make it clearly or changed on the comments based on two tests.


tommy you are totally wrong there is no relation of package statement and polymorphism with that package statement this program work greatly, even it'll work with any package tree except then there should be no use of java keywords to create any package tree.I think you should detailed study about package statement declaration.Can you tell me what do you feel wrong in that package statement ??
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Eli Wood wrote:,,,,,,,,,,,,,,,


thank you Eli for great explanation.

Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Nitesh Nandwana wrote:

and i am looking for more alternative ways of these declarations so i can easily understand this.



As no one answered about my this question so i've decided to post answer so other can also learn this.



there may be more other ways.
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Nitesh Nandwana wrote:
Dennis Deems wrote:...


Dennis don't worry after learning it i will post here in detail so everyone can learn it because noone have dare to explain it.


As i promised to Dennis and all ranches, i am gonna demo everone real polymorphism affect.



Output PlayerPiece>>displayingShape is result of real time polymorphism.Hope you like this post
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Nitesh Nandwana wrote:
Nitesh Nandwana wrote:
Dennis Deems wrote:...


Dennis don't worry after learning it i will post here in detail so everyone can learn it because noone have dare to explain it.


As i promised to Dennis and all ranches, i am gonna demo everone real polymorphism affect.


Uh ... thanks? You were repeatedly told that only overriding a method would achieve polymorphism. I guess you had to work it out for yourself.

Switch to decaf, ok champ?
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Glad that you've grabbed the Polymorphism concept, but this is just a scratch on the surface. Could you go further more...

1. Declare displayingShape() method in GameShape class as static, see what effect will happen, and find out why?
2. Declare an Exception on the original displayingShape() in GameShape class after you have tried 1, see what effect will happen and find out why?

This enough to warm up on topic and further...
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Tommy Delson wrote:

1. Declare displayingShape() method in GameShape class as static, see what effect will happen, and find out why?


You must have know that static method cant be overridden so your this point is useless.

Tommy Delson wrote:

2. Declare an Exception on the original displayingShape() in GameShape class after you have tried 1, see what effect will happen and find out why?



If exception is declared on original method then it wont affect on overriding method but if you try to declare exception for overriding although if that is not declare for original then it'll be your mistake.

Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

Dennis Deems wrote: You were repeatedly told that only overriding a method would achieve polymorphism. I guess you had to work it out for yourself.

Switch to decaf, ok champ?


Yes Overriding is essential but real magic start from main() you should not forget it

Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Nitesh Nandwana wrote:
Tommy Delson wrote:

1. Declare displayingShape() method in GameShape class as static, see what effect will happen, and find out why?


You must have know that static method cant be overridden so your this point is useless.

Tommy Delson wrote:

2. Declare an Exception on the original displayingShape() in GameShape class after you have tried 1, see what effect will happen and find out why?



If exception is declared on original method then it wont affect on overriding method but if you try to declare exception for overriding although if that is not declare for original then it'll be your mistake.



You getting there, that's the point I tried to make.

1. That's correct "You must have know that static method cant be overridden", but you missed one thing that a subclass can do with a static method. Do you know what is that?


2. Incorrect on Polymorphism, "If exception is declared on original method then it wont affect on overriding method" only for the unchecked exception, but not checked exception.

Example,

GameShape gs1=new PlayerPiece();
GameShape gs2=new TilePiece();


Here is why:


A recap from Exam Watch from K & B book Chapter 2, page 108

If a method is overridden but you use a polymorphic (supertype)
reference to refer to the subtype object with the overriding method, the compiler
assumes you’re calling the supertype version of the method. If the supertype version
declares a checked exception, but the overriding subtype method does not, the compiler
still thinks you are calling a method that declares an exception (more in Chapter 5).
Let’s take a look at an example:



This code will not compile because of the Exception declared on the
Animal eat() method. This happens even though, at runtime, the eat() method used
would be the Dog version, which does not declare the exception.


---------

Reread Chapter 2 on Object Orientation & Chapter 5 on Exception to reinforce your weakness or knowledge.

Try to test in code first to confirm and see how it behave before rush to conclusion.

Hope it help...



Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Dennis Deems wrote:
Nitesh Nandwana wrote:
Nitesh Nandwana wrote:
Dennis Deems wrote:...


Dennis don't worry after learning it i will post here in detail so everyone can learn it because noone have dare to explain it.


As i promised to Dennis and all ranches, i am gonna demo everone real polymorphism affect.


Uh ... thanks? You were repeatedly told that only overriding a method would achieve polymorphism. I guess you had to work it out for yourself.

Switch to decaf, ok champ?



I think so, that's the only way Nitesh Nandwana ables to grab the concept from the book and learn by himself. I'm glad that at last he knows how to prepare for the OCPJP.






We all trying to help here....close mind will never learn.

Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


In my opinion key advantage of polymorphism is that it gives you ability to write code which is extensible and more maintainable in long run. It works well with programming principle " programming with Interface rather than implementation", but yes until you provide a specific implementation in sub classes its of no use and affect the performance if you are working with time critical application as static binding is always faster than run time binding.



http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism problem