Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding java generic methods

Tags:

java

generics

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

like image 908
Xaq Avatar asked May 20 '13 19:05

Xaq


People also ask

How do you read Java generics?

“Java Generics are a language feature that allows for definition and use of generic types and methods.” Generic types are instantiated to form parameterized types by providing actual type arguments that replace the formal type parameters. A class like LinkedList<E> is a generic type, that has a type parameter E .

How is generic method implemented in Java?

Generic MethodsAll generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example). Each type parameter section contains one or more type parameters separated by commas.

What is the use of generic methods and generic classes in Java?

The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes the code stable by detecting the bugs at compile time. Before generics, we can store any type of objects in the collection, i.e., non-generic. Now generics force the java programmer to store a specific type of objects.


2 Answers

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.

like image 155
Keppil Avatar answered Sep 23 '22 23:09

Keppil


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)
like image 42
rgettman Avatar answered Sep 22 '22 23:09

rgettman