Java generics name clash , has the same erasure - java

I have superclass Foo. And a class Bar extending it.
public class Bar extends Foo
Function in Foo:
protected void saveAll(Collection<?> many)
Function in Bar:
public void saveAll(Collection<MyClass> stuff) {
super.saveAll(stuff);
}
Getting error :
Name clash: The method saveAll(Collection<MyClass>) of type Bar has the same erasure as saveAll(Collection<?>) of type Foo but does not override it.
What am I doing wrong?

You are overriding the saveAll method with an incompatible type. Perhaps you want to do something like:
public class Bar extends Foo<MyClass>
Function in Foo<E>
protected void saveAll(Collection<E> many)
and function in Bar:
public void saveAll(Collection<MyClass> stuff) {
super.saveAll(stuff);
}

Due to the type erasure feature of Java, the JVM will not be able to know whether it is the method that has the parametrized type MyClass or the first one that should be called.
If possible or applicable, the most commonly used pattern I've seen to avoid this is to change the class Foo to have a parametrized type as well:
public class Foo<T> {
protected void saveAll(Collection<T> many) {}
}
and then have Bar simply implement Foo for your specific type:
public class Bar extends Foo<MyClass> {
public void saveAll(Collection<MyClass> many) {
super.saveAll(many);
}
}

At runtime, the parameter types are replaced by Object.
So saveAll(Collection<?>) and saveAll(Collection<MyClass>) are transformed to saveAll(Collection). This is a name clash.
Look here for details.
You could do this :
public class Foo<T> {
protected void saveAll(Collection many) {
// do stuff
}
}
public class Bar extends Foo<MyClass> {
}

When the compilers compiles to byte code a process called Erasure happens. This remove the type information from the collections. I believe it will manually do the casts etc as part of the process of generating the byte code. If you remove the generic parts of your class (ie the <..> ) then you will see you have two saveAll methods. The error is that you have two save all methods will the same signature. The collections have type object in the byte code.
Try removing the <..> which might make it clearer. When you put the <...> back in then consider the name of the methods. If they are different it should compile.
Also I dont think this is a hibernate problem so this tag should be removed. It is a java generic problem you have.
What you could do here is type the class
public class Bar extends Foo<MyClass>
and then have the method types to T
public void saveAll(Collection<MyClass> stuff) {
super.saveAll(stuff);
}
and then the declaration of Foo would be something like
public abstract class Bar extends Foo<T> {
public void saveAll(Collection<T> stuff) {
}

You just overriding methods with different Signatures.
What will be good idea is to use PECS (Producer - Extends, Consumer - Super) rule described in Effective Java Second Edition by Joshua Bloch.
according to this rule it should looks like this.
In Foo class:
public class Foo<E>{
protected void saveAll(Collection<? super E> many){....}
protected void getAll(Collection<? extends E> many){....}
}

While the existing answers are correct, this error easily occurs when you declare the actual type not in the "extends ..." clause but in the actual class name:
Wrong:
public class Bar<MyClass> extends Foo
{
...
}
Correct:
public class Bar extends Foo<MyClass>
{
...
}

Related

trying to understand case of "both methods have same erasure, yet neither overrides the other" [duplicate]

This question already has answers here:
Method has the same erasure as another method in type
(8 answers)
Closed 4 years ago.
I'm trying to solve a problem and I've come up with this solution (simplified):
package help;
public class Problem {
private static class A<T> {
public void foo(T t) {}
}
private static class B<T> {}
private static class C<T> extends A<B<T>> {
public void foo(T t) {}
}
}
It wont compile since "foo(T) in help.Problem.C clashes with foo(T) in help.Problem.A; both methods have same erasure, yet neither overrides the other".
I'm not just trying to solve the problem, i also would like to understand what is going on. I noticed that if the B class is omitted, the error is gone.
Also: could you provide an example of a piece of code such, that the compiler wouldn't be able to bind a variable to one of those two methods?
The class definition of A means that the compiler "expects" something like this if you want to override foo:
private static class C<T> extends A<B<T>> {
public void foo(B<T> t) {}
}
However, since you're just providing T as a type to that method rather than B<T>, it's not a valid override of foo in class A - the parameter types must match. This wouldn't cause an error with non-generic types - you've just got an overloaded method (a method that has the same name, but differs in the type / number of its parameters) instead of an overriden one.
However, since the generic types disappear on compilation (through type erasure), this means you can't have two methods differentiated only by an generic type, as their definitions in the bytecode would be identical.
To see why they erase to the same type, just remove all <> and the stuff inside them, then replace all the generic parameter types with Object:
class Problem {
private static class A {
public void foo(Object t) {}
}
private static class B {}
private static class C extends A {
public void foo(Object t) {}
}
}
Now you see why.
Not being able to overload a method like this is one of the limitations of generics. Read more here.
The root of the problem is really that in C, T means something different than it does in A. Let's suppose you remove the declaration of foo in C and you have an instance of C<T>. You want to call foo using this instance. What parameter do you need to pass? A B<T>, right? If foo is declared to accept a T in C, that wouldn't override A.foo because A.foo need to accept a B<T>, not just T.
To actually override foo, change the parameter to be of type B<T>.

Generic(s) confusion: Type of this in generic superclass is not defined at compile time?

Just met this piece of code:
public class Container <T extends Containter<T>> {
protected Map<String, Rule> inspect (T t) {
// ....
}
public boolean isValid () {
// ...
inspect ((T)this);
// ...
}
}
What I'm confused about is the "isValid"-method.
When you replace the line with inspect(this); it won't compile: incompatible types: Container <T> cannot be converted to T.
In subclasses the error does not occur. So I'm asking myself: Could there be a situation where this is of Type other than Container? I mean what would be the type of this in the code above? The explicit cast seems to be unnecessary.
Hope you guys can understand me, I find it hard to explain stuff like that in english.
Thanks!
UPDATE
First, thank you and sorry for letting you wait.
There was confusion about the intention of the inspect method. It is supposed to work on the Container itself (not on the generic Type). Basically only subclasses of Container should be able to use it. So the whole thing is about inheritance.
It's about type safety for sub classes using the inspect method.
Imagine an instantiation like (inside the Container's inspect-method):
List<T> listOfSimilarContainers = new LinkedList<T>();
Now, if there was a subclass like
public class SubContainer extends Container<SubContainer> { }
calling inspect on that class should force the listOfSimilarContainers inside the inspect method to only contain SubContainers.
That is what I wanted to achieve.
Here is a simplified version:
interface Foo<T extends Foo<T>> { }
class Bar implements Foo<Bar> { }
class Baz implements Foo<Bar> { }
Note that Baz inherits from Foo<Bar> and not from Foo<Baz>. The code compiles without any errors.
In other words, the declaration of the interface does not guarantee, that T is the subclass of Foo<T>.
How should work inspect method? If you want to inspect the generic type, then T could be a Class<T> object, else inspect's signature sholud be:
public class Container<T extends Container<T>> {
protected Map<String, String> inspect(Container<T> t) {
}
public boolean isValid() {
// ...
inspect(this);
// ...
}
}

How to use subtypes? - overriding and inheriting in Java

I've got problem in my code in Java. I have four(important) Classes:
public class RDOutput extends OutputType
public class RDAnalysis extends AnalysisProperties
Now I'm trying to make a method in Analysis properties:
public abstract void display(ArrayList<? extends OutputType> results);
The main problem list, the objects in the ArrayList will be different subtypes of OutputType. In my class RDAnalysis I try to make specific overriding:
public void display(ArrayList<RDOutput> results) {
but eclipse says: Name clash: The method display(ArrayList) of type RDAnalysis has the same erasure as display(ArrayList? extends OutputType) of type AnalysisProperties but does not override it
I'm not familiar with Java tricks, I tried searching in documentation and I didn't find any solution to this problem.
My question is: Is that trick that I'm doing (Basic type in abstract and Extended in final function) possible in Java (if yes, how can I do that?) or do I have to make some enum to solve this?
I suggest you to introduce generic parameter to your class and use it to parametrize your method:
public abstract class A<T extends OutputType> {
public abstract void display(ArrayList<T> results);
}
public class B extends A<RDOutput> {
public void display(ArrayList<RDOutput> results) {}
}
It's because your display doesn't cover every case of the abstract method. Maybe try something like this :
public class RDOutput extends OutputType {}
public class OutputType {}
public abstract class AnalysisProperties<T extends OutputType> {
public abstract void display(ArrayList<T> results);
}
public class RDAnalysis extends AnalysisProperties<RDOutput> {
#Override
public void display(final ArrayList<RDOutput> results) {
}
}
The problem is that you try to override a method while restricting possible parameters.
=> ArrayList<? extends OutputType> accepts more possible elements than ArrayList<RDOutput> since RDOutput extends OutputType.
You break the rule that says: the concerned subclass method has to encompass at least elements of superclass one and NEVER restrict them.
So compiler avoid to valid this override.
By the way, avoid to type your reference with concrete values like ArrayList.
What about a LinkedList passed as arguments? ... prefer a more generic relevant type like List.
Problem here is that, after type erasure comes into play, the signature of the two methods are undistinguishable: they have the same return type and they can both accept a ArrayList<RDOutput> but the first one (the generic one) can also accept any ArrayList<T extends OutputType>.
This mean that, although the JVM won't be able to choose which one to call at runtime if you pass an ArrayList<RDOutput>, at the same time your display method does not override the abstract one because your method only work for lists of RDOutput, so if you pass a List<T extends OutputType> with T != RDOutput your specific implementation doesn't accept it.
You should consider using a type parameter on the whole class as suggested in other answers, or accept the fact that you won't be able to use any RDOutput specific methods in your display method without a cast.
if a method is expecting ArrayList<? extends OutputType>
ArrayList<RDOutput> cannot be passed to it, as parent type allows any child class of OutputType in arraylist.
consider a code like this
AnalysisProperties properties = new RDAnalysis();
properties.display(arraylist consisting of any child class of OutputType); //this line will cause runtime problems

Overriding non-parameterized classes nested in a parameterized outer class in Java

Can someone please help with the syntax of subclassing a non-parameterized nested class inside a parameterized outer class, like the following?
public abstract class Foo<T> {
public class Bar {
Set<T> aSet;
}
abstract class Baz {
abstract void doSomething(Map<? extends Bar, T> aMap);
}
}
Then in a separate file, I'm not entirely sure how to define the type variable S here without parameterizing ConcreteBaz. And I can't use a wildcard variable like extends Foo<?>.Baz (I think) because I need access to that generic type variable in the doSomething method:
public class ConcreteBaz extends Foo<S>.Baz { // compilation error
public ConcreteBaz(Foo<S> foo) { foo.super(); }
public void doSomething(Map<? extends Foo<S>.Bar, S> aMap) { ... }
}
Can someone please help me figure out a way around this? Thanks.
Declare S as a type parameter:
public class ConcreteBaz<S> extends Foo<S>.Baz {
Otherwise the compiler will think S is a concrete type instead of a type parameter.
Complete example:
public class ConcreteBaz<S> extends Foo<S>.Baz {
public ConcreteBaz(Foo<S> foo) {
foo.super();
}
#Override
void doSomething(Map<? extends Foo<S>.Bar, S> aMap) {
// ...
}
}
Your problem is that the nested class is nonstatic. I'll check it out, but I'm pretty sure you cannot subclass such classes, except maybe if nested in the same class, or when creating an anonymous type.
Is declaring the nested class static viable? Them it would definitely work.
Edit: scratch all that. I don't have a compilation error. What is S in your case? You do realize you have to pass a concrete class to Foo, and cannot leave it parametrized with an unknown parameter S?
If ConcreteBaz needs to refer to the type parameter, that implies that it needs the type parameter itself:
public class ConcreteBaz<S> extends Foo<S>.Baz {

Is it possible to write a method that returns a class object of T?

If I have a base class such that
public abstract class XMLSubscription <T extends XMLMessage>
Is it possible to write a method in XMLSubscription that returns a class object of T?
The only possible solution that I came up with is to have each descendant of XMLSubscription have a method like:
public class XMLStatusSubscription extends XMLSubscription<XMLStatusMessage>
{
public Class <XMLStatusMessage> getExpectedMessageType()
{
return XMLStatusMessage.class;
}
}
Unfortunately - and yes, this is due to type erasure - there is no way to return the Class object without providing it at runtime somehow.
Fortunately this is not usually too difficult. Here's how I've typically done this / seen it done:
public abstract class XMLSubscription <T extends XMLMessage> {
private Class<T> messageType;
protected XMLSubscription(Class<T> messageType) {
this.messageType = messageType;
}
public Class<T> getExpectedMessageType() {
return this.messageType;
}
}
public class XMLStatusSubscription extends XMLSubscription<XMLStatusMessage> {
public XMLStatusSubscription() {
super(XMLStatusMessage.class);
}
}
As you guessed, T is erased by the compiler. When you instantiate the object it has no idea that's it's supposed to deal with XMLStatusMessage objects. Your base class would define the following template method, and 1.5's covariant return types would keep the compiler happy with the concrete subclasses:
public Class<T> getExpectedMessageType()
There is one meta-comment: this looks a lot like procedural code, where something calls getExpectedMessageType() and then takes action based on the return type. This might be better implemented using a Visitor pattern, with the visitor implementing "doSomething" methods for each of the subclasses of XMLMessage.

Categories