aspose file tools
The moose likes Beginning Java and the fly likes Polymorphism issue in the test code Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Polymorphism issue in the test code" Watch "Polymorphism issue in the test code" New topic
Author

Polymorphism issue in the test code

Niccola LeBaron
Greenhorn

Joined: Apr 15, 2011
Posts: 4
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.

Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
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

Joined: Apr 15, 2011
Posts: 4
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

Joined: Nov 18, 2008
Posts: 371
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Polymorphism issue in the test code
 
Similar Threads
Where am I getting this from?
enhanced for loop hangup?
the instance variable shapes
constructor
Null Pointer Exception