permaculture playing cards*
The moose likes Java in General and the fly likes Method calling in class hierachy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method calling in class hierachy" Watch "Method calling in class hierachy" New topic
Author

Method calling in class hierachy

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi,

I have the classes below:



So assuming in class C, the method had no parameters, I would have in Parent: public abstract getValue();
So that code below works fine.



But since class C, requires a parameter, it complicates things.
I'm not sure of the correct way of handling this, I can think of a few ways, but they seem a little cumbersome, and I would really like keep the method name "getValue" the same, since it really does the same job in A, B and C.

Can anyone show me the 'official' way to handle this, if there is such a way.

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

can you put a getValue() method into C, and have it do something, even if it's only report "you can't call the getValue() method on this object without an int" kind of message?

or could you make all three classes have a getValue(int x), and ignore it in your A and B class? I don't really like this option - it seems sloppy, but it may solve your problem.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Well I still need to pass a value into the C, so even if I override getValue() in C, I still dont pass in the paramter.

I considered the other idea too, but its a bit sloppy, and the ide will warn me I have unused variables, so I'm not sure.

Thanks
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
You want to use the reference type as the "parent" and expects to send similar messages to all object it refers; no matter what the implementation class is.


Your issue is in choosing overriding vs overloading.
In overloading you have to use reference types of those classes where the method definitions are.
But in overriding you can use the reference type of the parent class.

So your question is how to convert overloading to overriding. The solution Fred explained above is one.

Another solution is to keep the parameter as a class variable and make sure it is set during construction. But what if this variable don't have anything to do with the state of the object C.

The actual issue is with type differentiation. Below is a similar example.




Here petalId is not representing the state of Flower. So it shouldn't be part of the state of Flower. But it is Ok to have Flower to have a method getPetal(int petalId) eventhough petalId is not used.

So if the parameter don't represent state, Fred's solution is better.
If the parameter represent state, my solution is better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method calling in class hierachy