This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Polymorphism (Overriding)

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
ScroogeXHTML 9.4 - fast RTF to HTML and XHTML conversion
https://coderanch.com/t/749563/ScroogeXHTML-fast-RTF-HTML-XHTML
reply
    Bookmark Topic Watch Topic
  • New Topic