Andrea
Andrea

Reputation: 178

error overriding method of class that extends generic class

i have a class A that extends a class B and the class B extends a generic Class

my class A is:

public class MyCustomerReviewConverter<SOURCE extends CustomerReviewModel, TARGET extends ReviewData> extends CustomerReviewConverter{

    @Override
    public void populate(final SOURCE source, final TARGET target) {.....}

the extended class B is

public class CustomerReviewConverter extends AbstractPopulatingConverter<CustomerReviewModel, ReviewData>{

    @Override
    public void populate(final CustomerReviewModel source, final ReviewData target)
    {..........}

but i'm getting the error

 Name clash: The method populate(SOURCE, TARGET) of type MyCustomerReviewConverter<SOURCE,TARGET> has the same erasure as populate(CustomerReviewModel, ReviewData) of type 
     CustomerReviewConverter but does not override it

what's wrong?

As second parameter in the populate method i have to pass a class

MyReviewData extends ReviewData{...}

thanks in advance

Andrea

Upvotes: 4

Views: 255

Answers (3)

Robby Cornelissen
Robby Cornelissen

Reputation: 97331

Tricky and difficult to explain.

  1. The type parameters are defined in the generic AbstractPopulatingConverter.
  2. The type parameters are concretized in CustomerReviewConverter.
  3. Then, you try to make the concretized type parameters generic again in MyCustomerReviewConverter.

And that just doesn't work. You're trying to override a method with a method that has a different method signature (different parameter types).

The only way you can override that method is as follows:

@Override
public void populate(final CustomerReviewModel source,
        final ReviewData target) { /* ... */ }

However, instead of overriding the method, you can overload it:

public void populate(final MyCustomerReviewModel source,
        final MyReviewData target) { /* ... */ }

If the populate() method is called on an object of type MyCustomerReviewConverter with a MyCustomerReviewModel and MyReviewData parameter, the compiler will select the most specific populate method, being the overloaded one.

Upvotes: 2

Ed Randall
Ed Randall

Reputation: 7600

The types of the generic types for <SOURCE, TARGET> have already been concreted in by B as <CustomerReviewModel, ReviewData>. If you want to extend B, your A should simply be:

public class MyCustomerReviewConverter extends CustomerReviewConverter {
    @Override
    public void populate(final CustomerReviewModel source, final ReviewData target) 
        { ... }
}

Otherwise you need to extend AbstractPopulatingConverter directly, and perhaps write a delegate to perform the shared logic within B so you can re-use it elsewhere.

@Robby Cornelissen explains it better than me in his answer though.

Upvotes: 1

llogiq
llogiq

Reputation: 14541

You should extend CustomerReviewConverter<SOURCE, TARGET> (probably, you haven't added its definition to the question) instead of the raw type. Then you need to override public void populate(SOURCE source, TARGET target).

Upvotes: 0

Related Questions