• 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

Polymorphism issue in the test code

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why it doesn't like the getName() call. this is my first post here so if I am missing something that I should have included please just let me know.



There is a super class called shapes and then two under that. TwoDimensionalShape and ThreeDimensionalShape
Under TwoDimensionalShape there are several subclasses. Circle, Square and triangle. Seems like everything is fine except the getName() call.
Here is what I have for one of the subclasses.

 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags.

The compiler uses the variable for determining if he can call the method. The variable you are calling getName on (currentShape) is of type Shape. So if Shape has no getName method it will not work, no matter whether the subclasses have such a method.

But of course the runtime will call the method of the current object and it's type.
 
Niccola LeBaron
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I figured out the code tags.

I added the getName method to shape and that fixed it. Thanks so much
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now your code is readable. In line 17 and 22 you create variables with the specific subtype. What about calling the methods on these variables? Not in every case you can change the interface of the super class.
reply
    Bookmark Topic Watch Topic
  • New Topic