Clash name, override fail, in a class that implements two interfaces with the same erase

I am creating a class that overrides the signature of the method, erasing which is identical between the two implemented interfaces, but with a slight difference in relation to the general type (one is the type defined by the method, the other type is inferred-class type), I am looking for a neat solution. I CAN ONLY edit the inherited class, and not the original deprecated interfaces.

To show this case, I compiled an abstract pattern to understand the problem:

I have a parent class of an outdated developer:

public class Developer<C>{
    Rate<C> getRate(Taxes<C> tax){ /*...*/ }     
}

I also purchased a Rentable legacy interface with an almost identical signature

public interface Rentable {
    <C> Rate<C> getRate(Taxes<C> taxes);  
}

Since the developer is not rented, in my model I create a special developer who is both a developer and a rented material.

public class OutsourcableDeveloper<C> 
                 extends Developer<C> 
                 implements Rentable{
   @Override
   public Rate<C> getRate(Taxes<C> taxes){ /*...*/}
}

clash: getRate (Developer.Taxes) OutsourcableDeveloper , getRate (Developer.Taxes) ,

, OutsourcableDeveloper.getRate() Developer, Rentable. getRate()?

, , , .

, , , ? , , , ?

EDIT: , , , , , , : " " ? "class generic" ,

EDIT2: ,

+4
2

, . clumpsy, , , " " ? " " ? :

public class OutsourcableDeveloper<C> 
                 extends Developer<C> 
                 implements Rentable{

    /* This might not be needed if we don't need to extract C from taxes parameter */
   final Class<C> currencyClass;
   public OutsourcableDeveloper(Class<C> currencyClass){ this.currencyClass = currencyClass;}

   @Override
   public Rate<C> getRate(@SuppressWarnings("rawtypes") Taxes taxes){
        try{
            C taxesCurrency = (C) currencyClass.cast(taxes.getCurrency()); //IF actually needed getting the typed instance
            return new Rate<C>(taxesCurrency); //Or whatever processing
        } catch (ClassCastException e){
            throw new UnsupportedOperationException("OutsourcableDeveloper does not accept taxes in a currency that its not hims");
        }
   }
}

"extends Developer" , . .

0

, . Rentable-Instance T, OutsourcableDeveloper .

, ,

<C> Rate<C> getRate(Taxes<C> taxes);  

. , , OutsourceableDeveloper. , getRate C, . → .

, , . . OursourcableDeveloperRentable, . , .

//This class itself can be added to any Developer-lists
public class OutsourcableDeveloper<C> extends Developer<C> {

    public final OutSourcableDeveloperRentable RENTABLE_INSTANCE = new OutSourcableDeveloperRentable();

    @Override
    public Rate<C> getRate(final Taxes<C> taxes) {
        // Simply forward to the more general getRate instance.
        return this.RENTABLE_INSTANCE.getRate(taxes);
    }

    public void exampleBehaviourA() {
        //Example for how you can make both objects behave equally.
    }

    // This class can be added to the lists requiring a Rentable
    // And the original value can be retrieved by both classes.
    public class OutSourcableDeveloperRentable implements Rentable {

        public final OutsourcableDeveloper<C> PARENT_INSTANCE = OutsourcableDeveloper.this;

        //This method is the one to implement because it is more general than
        //the version of OutsourcableDeveloper.
        @Override
        public <T> Rate<T> getRate(final Taxes<T> taxes) {
            // Do your work.
            return null;
        }

        public void exampleBehaviourA() {
            //Just an example - Maybe for you it makes for sence to
            //forward the method of Oursoursable-Developer to here.
            //Then all Behaviour would be found in this class.
            OutsourcableDeveloper.this.exampleBehaviourA();
        }

    }
}
+2

Source: https://habr.com/ru/post/1619639/


All Articles