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

Polymorphism doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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.


Tell the difficulties that i am difficult.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Dec 08, 2010
Posts: 1512
    
    5

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

Joined: Feb 09, 2012
Posts: 583

Can anyone help me ? please


Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

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

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:


hmm.. thanks Anayonkar


Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

You are welcome.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism doubt