• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Polymorphism problem

 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is this correct program of polymorphism ?

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

just try

and try again to run.

And continue thread with your opinion.

Thanks.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polymorphism means that a subclass can override a method defined in the superclass and specify its own behavior.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See my comments below, and I let the rest for you to understand why...hope it help.


 
Eli Wood
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



thank you Eli for great explanation.

 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic