• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism (Overriding)  RSS feed

 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!