Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method calling in class hierachy

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic