Method with Generics in inherited type

Asked

Viewed 67 times

1

How do I use two types in the method Generics, to sum only the areas of the classes Quadrado and Retangulo.

I tried a method like this:

public static double soma(Forma <? extends Quadrado> elementos) {


        return elementos.getArea();

    }

and Forma and abstract.

  • Talk more about the classes Forma, Quadrado and Retangulo. What are their methods? The solution that I would come up with would simply not use any generic type, so I would like to see your classes to understand what these generic types are and what they represent.

1 answer

1

This is not possible in Java.

Quadrado and Retangulo inherit from Forma? Or does one inherit from the other? Well, if so, is a classic mistake. But let’s consider the first.

public static <T extends Forma> double soma(T elementos) {
    return elementos.getArea();
}

I put in the Github for future reference.

What should do within the method I do not know since there is no information about it.

  • And where is there something being added up in the method soma?

  • @Victorstafusa I answered about the problem that of course, maybe I should close the question as unclear.

  • I already gave the first closing vote.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.