Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Polymorphism - please help me understand

Thread view: 
Mike B - 08 Jan 2006 04:10 GMT
First off, I am apologizing. Another very similar post of mine will
eventually appear on the newsgroup. I made it several hours ago using Google
Groups and it got stuck somewhere. Eventually it'll also appear here.

I'm reading in a textbook about Java and in the chapter about polymorphism
it says the following: "One benefit of polymorphism is that you can write
generic code that's designed to work with a superclass. Then you can use
that code with instances of any class that's derived from the superclass.
for example, suppose you have a ProductDB class with an addRecord method
that accepts a Product object as a parameter. Because the Book and Software
classes are both derived from the Product class, the addRecord will work
with Book and Software objects".

How can this be true? In the Book and Software objects are data fields
(variables) that are not in the Product object. Surely it cannot preserve
fields it has no knowledge about? As an example, if I have a toString method
in the Product class, I must still write a toString method in the
Book/Software classes to return printable representations of the variables
in those classes?

What am I missing here?

Thanks
Signature

Mike B

Chris Smith - 08 Jan 2006 04:26 GMT
> How can this be true? In the Book and Software objects are data fields
> (variables) that are not in the Product object. Surely it cannot preserve
> fields it has no knowledge about?

The ProductDB class will do what its code tells it to do.  If that code
reads and stores individual fields, then it will only store those fields
that it's programmed to read and store.  For that reason, this
ProductDB.addRecord is probably a bad example.  It would be possible to
make it work with polymorphism, but it would require writing knowledge
about the database into the Book and Software classes, which is
generally considered a bad idea.

> As an example, if I have a toString method
> in the Product class, I must still write a toString method in the
> Book/Software classes to return printable representations of the variables
> in those classes?

Yes, you absolutely do need to do that. Polymorphism says that if I have
a function like this:

   public void printToLog(Product p)
   {
       logPrinter.println(p.toString());
   }

Then I can do either:

   printToLog(myBook);
   printToLog(mySoftware);

and the toString() that's called from printToLog WILL be the toString()
that was defined in Book or Software, not the one defined in Product.  
It's not magic; you still have to write the code... but when you write
the code for Book, for example, it can be called by someone who doesn't
necessarily know that they are working with a Book.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 08 Jan 2006 09:57 GMT
> Surely it cannot preserve
>fields it has no knowledge about?

Yes it does.  Under the covers you can think of a  Dalmatian record as
a Dog record with a few fields tacked on the end.  The Dog code just
uses the dog fields, but passes the address of the complete beast
around.  When it gets past to something that understands Dalmatian
code it will use the Dalmatian fields.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Mike B - 08 Jan 2006 13:22 GMT
>> Surely it cannot preserve
>> fields it has no knowledge about?
[quoted text clipped - 4 lines]
> around.  When it gets past to something that understands Dalmatian
> code it will use the Dalmatian fields.

Thanks, so that means addRecord can add the record to the data base just
fine and then when I use getRecord from the ProductDB  I just have to
determine with getClass from Object what class it is and I can cast it as a
Book or as Software?

Signature

Mike B

Chris Smith - 08 Jan 2006 22:33 GMT
> > Yes it does.  Under the covers you can think of a  Dalmatian record as
> > a Dog record with a few fields tacked on the end.  The Dog code just
> > uses the dog fields, but passes the address of the complete beast
> > around.  When it gets past to something that understands Dalmatian
> > code it will use the Dalmatian fields.

That's not polymorphism at all.  If you pass the object to something
that uses a Dalmation, you would have to cast the object.  Polymorphism
says that if you call a method of that object, and Dalmation has
overridden (i.e., provided a specialized version of) that method, then
the specialized version of the method will be called... and will already
know that it's a Dalmation.

In Java, polymorphism never has anything to do with passing an object to
some other it of code.  It only applies to doing something to the object
itself.

> Thanks, so that means addRecord can add the record to the data base just
> fine and then when I use getRecord from the ProductDB

Not necessarily.  Roedy is talking about uses of the object INSIDE the
language.  Polymorphism does not solve the problem of storing an object
in an external database.  (Reflection, on the other hand, might help to
solve that problem of storing the object to an external database... but
even reflection is generally supplemented by some kind of annotations or
customizable O/R mapping configuration file when solving that problem in
the real world.)

> I just have to
> determine with getClass from Object what class it is and I can cast it as a
> Book or as Software?

You can do this, but that's not polymorphism either.  That's runtime
type identification, which is a different matter.  Typically, you'd do
it with the instanceof keyword, rather than with getClass().

Polymorphism says that if you call a method of that object that's been
overridden by Software, then the overriding code will get called (and
THAT code will implicitly know that it's working on an object of class
Software, since that's where it's defined).

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.