• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polymorphism doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am completely confused. What is actually polymorphism ?
---------------------------------------------------------------------------
from K&B
Certification Objective —Polymorphism (Exam Objective 5.2)

Remember, any Java object that can pass more than one IS-A test can be considered polymorphic.
----------------------------------------------------------------------------------------------------------------
Now in this below example from oracle,there is no more than one IS-A relationship, then how they are achieving polymorphism, then what is meaning of above K&B rule ?


Polymorphism can be demonstrated with a minor modification to the Bicycle class. For example, a printDescription method could be added to the class that displays all the data currently stored in an instance.

To demonstrate polymorphic features in the Java language, extend the Bicycle class with a MountainBike and a RoadBike class. For MountainBike, add a field for suspension, which is a String value that indicates if the bike has a front shock absorber, Front. Or, the bike has a front and back shock absorber, Dual.
Here is the updated class:

Note the overridden printDescription method. In addition to the information provided before, additional data about the suspension is included to the output.

Next, create the RoadBike class. Because road or racing bikes have skinny tires, add an attribute to track the tire width. Here is the RoadBike class:


Note that once again, the printDescription method has been overridden. This time, information about the tire width is displayed.

To summarize, there are three classes: Bicycle, MountainBike, and RoadBike. The two subclasses override the printDescription method and print unique information.

Here is a test program that creates three Bicycle variables. Each variable is assigned to one of the three bicycle classes. Each variable is then printed.

The following is the output from the test program:

Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10.

Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10.
The MountainBike has a Dual suspension.

Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20.
The RoadBike has 23 MM tires.

The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:there is no more than one IS-A relationship


Well, MountainBike IS-A Object, and MountainBike IS-A Bicycle.

In other words, MountainBike can take two forms(i.e. two identities). In one, you can call methods from Object class over it (e.g. toString) and in another form, you can call methods from Bicycle class (printDescription).
Further, at code level, you can pass MountainBike object to all methods which expects
1) Object object
2) Bicycle object
3) MountainBike object

I hope this helps.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

saloni jhanwar wrote:
I hope this helps.



This below code is from k&b and this is an example of more than one IS-A relationship through inheritance and interface implementation.


Now whereas in oracle example


So i think more than one IS-A possible only using both inheritance and interface implementation and there is nothing like that in oracle example.

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:So i think more than one IS-A possible only using both inheritance and interface implementation and there is nothing like that in oracle example.


In that case, I would stick to Oracle's example
Even I don't know where Sierra-Bates found that definition (more than one IS-A relationships) . At least I could logically deduce it like IS-A Object and IS-A another class.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me ? please


 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:Can anyone help me ? please


What part you are finding difficult? I just went through Oracle's page about polymorphism and my previous post:

Anayonkar Shivalkar wrote:In other words, MountainBike can take two forms(i.e. two identities). In one, you can call methods from Object class over it (e.g. toString) and in another form, you can call methods from Bicycle class (printDescription).


is almost complying to that.
I think that to demonstrate polymorphism, extending a single class is sufficient.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:



hmm.. thanks Anayonkar


 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

saloni jhanwar wrote:there is no more than one IS-A relationship


Well, MountainBike IS-A Object, and MountainBike IS-A Bicycle.

In other words, MountainBike can take two forms(i.e. two identities). In one, you can call methods from Object class over it (e.g. toString) and in another form, you can call methods from Bicycle class (printDescription).
Further, at code level, you can pass MountainBike object to all methods which expects
1) Object object
2) Bicycle object
3) MountainBike object

I hope this helps.


Object class methods are inherited by its sub classes and hence the toString() is available to Bicycle & Bicyle's subclass MountainBike. I think it's irrelevant to take it as a polymorphism example.

The polymorphism shown in the example is that the Bicycle reference can refer to either Bicycle object or MountainBike object or a RoadBike object during runtime.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic