lundi 19 septembre 2022

how to force the children method arguments to extend specific class for multiple methods in java

I have an interface:

public interface Parent{
  Integer method1(Payment payment);
  Integer method2(Invoice invoice, Payment payment);
}

And I have multiple kinds of classes that extends Payment and Invoice class, let's say PaymentType1, InvoiceType1 and PaymentType2, InvoiceType2 are my children classes of Payment and Invoice.

Now, I want to create two classes that extend Parent in a way to force child classes to just use the extended class of Payment and Invoice. For example something like this:

First:

public class ChildType1 extends Parent{
  public Integer method1(PaymentType1 payment){...}
  public Integer method2(InvoiceType1 invoice, PaymentType1 payment){...}
}

Second:

public class Child2 extends Parent{
  public Integer method1(PaymentType2 payment){...}
  public Integer method2(InvoiceType2 invoice, PaymentType2 payment){...}
}

what kinds of solutions can I use to resolve it?

Aucun commentaire:

Enregistrer un commentaire