File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Calling methods using dynamic binding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling methods using dynamic binding" Watch "Calling methods using dynamic binding" New topic

Calling methods using dynamic binding

Ryan Speller

Joined: Mar 01, 2008
Posts: 9
ok this one has got me lost and I just cant seem to figure it out at all.

basically I have a program where a user creates three shapes and they are then displayed in the order inputted and then the program is suppose to oranize them by area, smallest to biggest.
This chapter has to do with polymorphism and inheritance if that helps at all.
I have 3 shapes I can make, Circle, Rectangle, and a Square.
The base class is called Shape.
The program that these classes actually run in is this:

so what has me lost is, how do I organize the shapes in the last part of the program by calling the area() method using dynamic binding?

BTW, I have read the small section on dynamic binding in my book (Java, Savitch 2008) and my professors notes and I am totally lost. So I'd appreciate it if you could keep it simple.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I assume that you have an abstract method called "getArea" (or similar) in class Shape, correct? In that case, "slist[0].getArea()" would invoke whichever method is appropriate for the shape at position 0.

Are you supposed to implement your own sorting method? If not, you can use the java.util.Arrays.sort(Object[]) method. All you'd need to do would be to have each shape implement the Comparable interface, and use the objects' area in the compareTo method.
[ March 14, 2008: Message edited by: Ulf Dittmer ]
I agree. Here's the link:
subject: Calling methods using dynamic binding
It's not a secret anymore!