0

My IDE (IntelliJ) warns me about an unchecked conversion:

Unchecked overriding: return type requires unchecked conversion. Found 'com.company.Main.Cat', required 'T'

Nevertheless my code works as expected. Is there something I can do to prevent this warning or does my code have a "design" flaw?

The following MCVE reproduces the warning in public Cat copy() for Cat.

   public static void main( String[] args )
   {
      Cat cat = new Cat( "Mimi" );
      Cat copyCat = cat.copy();
   }

   public static class Cat extends Animal
   {
      public String name;

      public Cat( final String name )
      {
         this.name = name;
      }

      @Override
      public Cat copy() // Unchecked conversion warning
      {
         return new Cat( this.name );
      }

   }

   public abstract static class Animal
   {
      abstract < T extends Animal > T copy();
   }

2 Answers 2

5

Your Animal.copy method is generic, suggesting that the caller can specify the type of animal they want... but your implementation isn't generic.

So for example, here's a slightly augmented and changed version of your code - note the change to the main method, which uses just Animal as the variable type, but specifies a type argument for the copy method:

public class Test {
    public static void main(String[] args) {
        Animal cat = new Cat("Mimi");
        Dog copyCat = cat.<Dog>copy();
    }

    public static class Cat extends Animal {
        public String name;

        public Cat(final String name) {
            this.name = name;
        }

        @Override
        public Cat copy() {
            return new Cat(this.name);
        }

    }

    public abstract static class Dog extends Animal {
    }

    public abstract static class Animal {
        abstract <T extends Animal> T copy();
    }
}

The main method compiles without warning - it's fine.

But at execution time:

Exception in thread "main" java.lang.ClassCastException: Test$Cat cannot be cast to Test$Dog
        at Test.main(Test.java:6)

So yes, your code has a design flaw, and only works as expected in the use case you started with. You almost certainly don't want to make the method generic. You could either make the class generic, or just declare the abstract copy method as returning Animal:

public abstract static class Animal {
    abstract Animal copy();
}

Now that would allow Dog.copy() to be declared to return Cat of course... but it's at least simple.

0
1

If you want a generic copy() method to work the way you expect it, I'd change the two classes like that:

public abstract static class Animal<T extends Animal> {
   abstract T copy();
}

public static class Cat extends Animal<Cat> {
    public String name;

    public Cat(final String name) {
        this.name = name;
    }

    @Override
    public Cat copy() {
        return new Cat(this.name);
    }
}

This way, the Animal class can have generic methods, and the subclasses like Cat, knowing what type to insert for the type variable T, specify concrete classes.

Now, Animal.copy() is known to return an Animal (exactly: some type T that extends Animal), and Cat.copy() a Cat, exactly what we expect.

On method generics

One remark on generic type parameters to methods, as in your version of the Animal.copy() method. If you don't have a method argument with that variable type, it's an error in 99% of cases.

A method like

    public <T extends Animal> T copy(T originalAnimal) { ... }

can be okay. The compile-time type of the argument then tells the compiler what to expect as result, and it's possible (e.g. using clone()) to implement a method that returns an instance of the same class that it got as its argument.

But with a method like

    public <T extends Animal> T copy() { ... }

it's already hard for the compiler to determine the T type (sometimes it's possible from the variable where you assign the value into, or you can supply the <Cat> type infix).

But even more, you don't know the T type at runtime, and don't have any info available in your method implementation whether T is somehow bound to Cat, Dog or whatever. So you can't implement a method that follows this signature.

Rule of thumb: prefer generic type parameters to classes over those to methods.

1
  • Thanks for your additional clarification, i ended up implementing generic types to classes as you proposed. Before I struggled to understand the issues to determine the type of T for generic methods.
    – alex
    Dec 3, 2020 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.