5

I'm trying to understand generic methods in Java. Given the following code:

public class GenericTest {

    interface Shape {
        public long area();
    }

    public static class Triangle implements Shape
    {
        private long base, height;
        public long area() { return (base * height) / 2; }
    }

    public static  class Rectangle implements Shape
    {
        private long width, height;
        public long area() { return width * height; }
    }

    public <T extends Shape> long area1(T shape)
    {
        return shape.area();
    }

    public long area2(Shape shape)
    {
        return shape.area();
    }

}

I can't see/understand why I should use/implement area1 instead of area2 (or vice versa). Am I missing something? Don't both methods do the same thing?

It has left me a bit confused regarding generics in Java

4
  • 1
    It makes no sense to use generics in this case. Cases in which it does make sense are, when you don't know what kind of types you have to store. Commented May 20, 2013 at 19:08
  • 1
    In this case as @LukasKnuth said you needn't create a generic version of the area area2() is acceptable; However the current oracle documentation shows great examples on generics. The wildcard generics examples may also help clarify the confusion .. .have a look at docs.oracle.com/javase/tutorial/extra/generics/methods.html Commented May 20, 2013 at 19:13
  • I was looking at generics as a rough equivalent to C++ templates. The key thing you pointed out here that is lacking from the other answers (and quite possibly what I as missing is the concept that they are used "when you don't know what type to store." Which is exactly what I was looking for in the answer :-) Commented May 20, 2013 at 19:14
  • 1
    @AhmedMasud, I looked at that information and was under the impression having read that page that the extends type portion was not necessary -- which resulted in a great deal of confusion, prompting my previous question: link And seeing that the problem could be resolved without generics at all, I posted this question to determine why I would ever want to use them if I always had to know the type (unlike a C++ template).The answer given by @LukasKnuth that filled in the missing piece of the puzzle Commented May 20, 2013 at 19:26

3 Answers 3

13

In your example, since the T type parameter isn't used in any return values, there is no difference.

However, imagine you had the following methods:

public <T extends Shape> T movedShape1(T shape) {
    return shape.move();
}

public Shape movedShape2(Shape shape) {
    return shape.move();
}

Here you can see a clear advantage to using movedShape1(). You get a more specific type for your return value, without losing any type safety.

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't that still the same? I don't see the extra type safety, they both return Shape don't they?
@Xaq: If you have a class Circle implements Shape, then calling movedShape1() with a Circle parameter would return a Circle, not a Shape. Calling movedShape2() with the same Circle parameter would yield a Shape.
@Xaq, Keppil is right, and that's super useful when you call a method like that.
4

There is no good reason for creating the area1 method. The area2 method is preferable. Generics are to be used when there is a relationship with a specific yet unknown type. Here, there is nothing specific about the parameter shape. The interface Shape already lets us use the area method, so we don't care which specific implementation of Shape is passed in as shape. So generics aren't needed here.

Use

public long area2(Shape shape)

Comments

0

I this case - Shape is not a (abstract)Class but an Interface, so it has to be implemented (extended in gnerics form of thinking) - I'd say they do exaclty the same, and adding generics there is only shadowing what's really going on

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.