• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling methods using dynamic binding

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic