Reputation: 1436
i have a question regarding method-overriding which probably is influenced by some other aspects so i hope the question is sufficiently specific. Context: i try to construct a simple calculator for prices of certain objects BUT the "kind of price" shall be decoupled from the rest of the logic so that the client only needs to know the following interface.
I think it is best explained with the basic code where my problem starts.
public interface InterfacePrice {
public InterfacePrice addPrices(InterfacePrice price1, InterfacePrice price2);
public InterfacePrice subtractPrices(InterfacePrice price1, InterfacePrice price2);
public String priceToString();
}
So this is what i should be able to do with two prices. Now i tried to specify this some more:
public class BnSPrice implements InterfacePrice {
int gold = 0;
int silver = 0;
int copper = 0;
//the following will obviously not compile but i hope it helps explain
@Override
public InterfacePrice addPrices(BnSPrice price1, BnSPrice price2) {
return null;
[...more code here...]
}
So what i would like to do is write a simple add-method based on the fact 100 copper are one silver and so on. Now my problem: the compiler won't allow me to change the parameters to type BnSPrice which i understand, i just want to illustrate the problem. It doesn't make sense to try adding some golds and silvers of two concrete prices if i don't know they have the right type so the first thing that comes to my mind to get the correct behaviour is overriding the method with the normal InterfacePrice parameters and making a typecheck at the begin of the method and casting to the propper type but that seems not very efficient or beautiful.
Question: how can i change the design to get the flexibility to add other price models later while my client-code only needs to know the interface? (I am just learning java so plese forgive me if the question has some obvious answer i don't see.) Can it be solved by using generics? (I don't know much about those...i'm struggeling to get the usage of generics into my mind.)
Thanks in advance for any help.
Upvotes: 3
Views: 187
Reputation: 131346
As you want to apply in derived classes, some constraints on the parameters of parent methods, Generics could help you.
With Generics, you can also choose to return in the method the parameterized type or InterfacePrice
.
As in your concrete class example, you write :
@Override
public InterfacePrice addPrices(BnSPrice price1, BnSPrice price2) {
I will give an example that follows this way for the addPrices() method and for the subtractPrices() method, I will use the way where we want to return the specific type .
You could create a Generic interface:
public interface InterfacePrice<T extends InterfacePrice<T>> {
public InterfacePrice<T> addPrices(T price1, T price2);
public T subtractPrices(T price1, T price2);
public String priceToString();
}
And here is a implementation of it :
public class BnSPrice implements InterfacePrice<BnSPrice> {
int gold = 0;
int silver = 0;
int copper = 0;
@Override
public InterfacePrice<?> addPrices(BnSPrice price1, BnSPrice price2) {
return ...;
}
@Override
public BnSPrice subtractPrices(BnSPrice price1, BnSPrice price2) {
return ...;
}
@Override
public String priceToString() {
return ...;
}
}
Upvotes: 3