Difference between generic super class and super class type
Asked Answered
C

4

20

I can't understand the difference between the two code snippets below. Can someone help me with a simple explanation?

First of all, I have to say that I have a lot of classes that extend a super class named BaseEntity, so what are the differences, benefits and drawbacks of the following snippets?

// 1
public <T extends BaseEntity> T getName(T t) {
    return t;
}

// 2
public BaseEntity getName(BaseEntity t) {
    return t;
}
Crotchety answered 9/12, 2015 at 8:38 Comment(0)
B
25

The first snippet is more flexible as it preserves the actual type of T. Suppose you have a subclass:

class SubEntity extends BaseEntity {}

In the first case you can write:

SubEntity result = getName(new SubEntity());

But in the second case you will need a cast:

SubEntity result = (SubEntity)getName(new SubEntity());
Baziotes answered 9/12, 2015 at 8:44 Comment(0)
A
7

The main difference when using the two methods is the possible need for casting in the second case.

Let's say you have:

public class MyEntity extends BaseEntity {
}

With the first method you can have something like:

MyEntity myEntity = ...
MyEntity entity = getName(myEntity);

While with the second method you would have to write:

MyEntity entity = (MyEntity)getName(myEntity);

This is because you specify the concrete type in your first method.

Aggravate answered 9/12, 2015 at 8:46 Comment(0)
D
4

I am surprised that nobody has mentioned this in the previous answers, but there is a more fundamental difference between the two method declarations (which is the reason for the need for casting in the second case). This difference is irrelevant for the trivial methods that you have provided here but it could make a difference for a method that does something different from simply returning its argument.

Your first method declaration requires that the return type be the same type as the argument passed in. So

public <T extends BaseEntity> T getName(T t) {
    return new SubEntity(); // Where SubEntity extends BaseEntity
}

fails to compile whereas

public BaseEntity getName(BaseEntity t) {
    return new SubEntity(); // Where SubEntity extends BaseEntity
}

is totally legal even if the BaseEntity passed into the method is a completely different type from SubEntity.

Drakensberg answered 9/12, 2015 at 14:52 Comment(0)
K
1

1)In your first code you have a Restrictions of return method should be subclasses of BaseEntity and the input param must be the same subclass of BaseEntity.

2)In your second code you have the return and param of method should be subclasses of BaseEntity.

Kery answered 9/12, 2015 at 8:44 Comment(1)
That's not true. In the first code snippet, the type of the input param must be the same subclass of BaseEntity as the return value. It can't just be any type of object. In the second snippet, the return value and input parameter can also be subclasses of BaseEntity.Drakensberg

© 2022 - 2024 — McMap. All rights reserved.