Java – How do I specify method parameters when rewriting without casting?

How do I specify method parameters when rewriting without casting?… here is a solution to the problem.

How do I specify method parameters when rewriting without casting?

I

have a question about method coverage, which may be affected by some other aspects, so I hope this issue is specific enough.
Context: I’m trying to build a simple calculator for the price of some objects, but the “price kind” should be separated from the rest of the logic so that the client only needs to know the following interface.

I think it’s best to explain it with the codebase that starts with my question.

public interface InterfacePrice {
    public InterfacePrice addPrices(InterfacePrice price1, InterfacePrice price2);
    public InterfacePrice subtractPrices(InterfacePrice price1, InterfacePrice price2);
    public String priceToString();
}

So that’s what I should be able to do for two prices.
Now I’ll try to elaborate:

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 want to do is write a simple addition based on the fact that 100 copper equals 1 silver, etc. Now my question:
The compiler doesn’t allow me to change the argument to the BnSPrice type I understand, I’m just trying to illustrate. It doesn’t make sense to try to add some gold and silver at two specific prices if I don’t know they have the correct type, so the first thing that comes to my mind is to override the method parameters with plain InterfacePrice and do a type check at the start of the method and convert to the appropriate type, but that doesn’t seem very efficient or aesthetically pleasing.

Question: When my client-side code only needs to know the interface, how do I change the design to get the flexibility to add additional price models later?
(I’m just learning Java, so forgive me if there are some obvious answers to this question that I don’t see.) Can it be solved by using generics? (I don’t know much about those…) I’m trying to get generic usage into my head. )

Thank you in advance for your help.

Solution

If you want to apply in a derived class, with some restrictions on the parameters of the parent method, generics can help you.

With generics, you can also choose to return a parameterized type or InterfacePrice in a method.

In your concrete class example, you write:

@Override
public InterfacePrice addPrices(BnSPrice price1, BnSPrice price2) {

I’ll give an example of following this for the addPrices() method and the subtractPrices() method, and I’ll use the way we want to return a particular type.

You can 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();
}

Here is one 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 ...;
    }
}

Related Problems and Solutions