| Author |
Polymorphism (Overriding)
|
John Park
Greenhorn
Joined: Aug 04, 2005
Posts: 26
|
|
Hey all, I understand what happens below: class Foo{ public void eat() {} } class Foo2 extends Foo{ public void eat() {} } class TestFoos{ public static void main (String [] args){ Foo f = new Foo(); <b>Foo f1 = new Foo2(); f1.eat(); } } </b> I know that at runtime, f1 will invoke Foo2's eat() method, assuming they both have same level access modifier. But why? what is the benefit of creating a Foo2 instance to a reference to Foo? Thanks, JP
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
the classic example has to do with animals. say you have a mammal class. you create a dozen or so subclasses, like dog, cat, bear, rhino, etc. you can now create an array (or other collection) of Animals, but stick in a dog, cat, bear or rhino. when you get them out, or just iterate over all of them, you can just call animals[i].run(), and the correct version will be called for each.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
Oh, that's like saying "Why should you drive a Mercedes when you can drive any old car?" or "why should you eat a steak, when any old protein source will do." Maybe your Foo can drive you somewhere. Any old, drivable foo will do, but who wants to drive in any old foo? I like exotic, German or Italian foos myself. When we code, we like to code to an interface, and make sure everything works with the most simple and basic types of objects. If you need to get home from the airport, a limo would be nice, but if the Canadians cut off the oil pipelines, you may need to take an electric train or a rickshaw. The point is, coding to the interface or most general aspect makes things the most flexible. But at runtime, you can supply the most specific, best type of object you can think of that will do the job the best. That's the beauty of polymorphism. Now if everythings good, I'm going to jump in my BMW and grab some dinner at Ruth's Chris Steakhouse.
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
John Park
Greenhorn
Joined: Aug 04, 2005
Posts: 26
|
|
okaaay...crazy example, but i think i understand a bit better... so, with the example of the previous example above of going home from the airport: //Assume Transport is an interface with signature goHome(), and that the Limousing, BMW, and Yugo classes are implementing Transport.. if(money > 1000) Transport ride = new Limousine(); ride.goHome(); else if (money > 500) Transport ride = new BMW(); ride.goHome(); else Transport ride = new Yugo(); ride.goHome(); I hope i didn't confuse anyone....thanks, JP
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Originally posted by John Park: okaaay...crazy example, but i think i understand a bit better... so, with the example of the previous example above of going home from the airport: //Assume Transport is an interface with signature goHome(), and that the Limousing, BMW, and Yugo classes are implementing Transport.. if(money > 1000) Transport ride = new Limousine(); ride.goHome(); else if (money > 500) Transport ride = new BMW(); ride.goHome(); else Transport ride = new Yugo(); ride.goHome(); I hope i didn't confuse anyone....thanks, JP
Alternatively you could write;
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Garrett Rowe: Alternatively you could write;
Exactly! And then you could change it to In fact, this is very close to how, for example, JDBC works: Only that the code in DriverManager is much more dynamic than a hard coded if-else chain.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Srinivas Kalvala
Ranch Hand
Joined: Oct 20, 2005
Posts: 257
|
|
Hai, The simple thing is hiding the object creation or making the object creation out of application or one point way to create objects. That is Factory Pattern. With this one can control the object creation at sub class level. --- And with overridding, take following example, Assume we have top level Image interface, and circle, rectangle, Triangle all implement that interface, Now assume there is one method in the interace draw() implemented by all classes. Now at runtime, you can assign any class and just call draw() then corresponding image will be drawn. like, Image imgae=new Circle(); image.draw(); or with factory pattern Image image=ImageFactory.getImage("parameters"); image.draw(); the factory will abstract every thing being drawn at runtime. Hope you got it.
|
 |
 |
|
|
subject: Polymorphism (Overriding)
|
|
|