Java generics type mismatch?

The main problem is because you can't cast Box catBox = tigerBox; catBox. ThrowHere(new BlackCat()) and then, the tigerBox is corrupted Let me reword your question as: public abstract class Company { public Box getFavBox() { // ... } public Set getBoxes() { // ... } } Company o = new ETCatCompany(); Box boxes = o.getFavBox(); // ok Set boxes = o.getBoxes(); // error As you see o.getFavBox() will work but o.getBoxes() not, why? Because, you could not throw any concrete Cat into Box Cat Box ^ BigBox => Box Box ^ Box => Box Box ^ BigBox => (Box ^ BigBox) ^ (BigBox ^ BigBox) => Box ^ BigBox => Box ^ BigBox => Box Set ^ Set => Set Set ^ Set => Set Set ^ Set => Set?

Extends Cat ^? Extends Tiger =>? Extends Cat Box ^ BigBox => Box Box ^ Box =>?

Extends Box =>? Extends Box Set ^ Set => Set Set ^ Set // You are here. => Set Set ^ Set => Set> x1 = o.operation(); Map> x2 = o.operation(); ... Map> x2 = o.operation() Then, what's the common type of all?

X1 ^ x2 ^ ... ^ xn => Map> ^ Map> ^ ... => Map> ^ ... ... => Map.

You can think it as, A Box can contain cats and tigers, but a Box can contain only tigers. If you cast a Box to Box, and later you throw a BlackCat into the Box: Box catBox = tigerBox; catBox. ThrowHere(new BlackCat()); and then, the tigerBox is corrupted.

Let me reword your question as: public abstract class Company { public Box getFavBox() { // ... } public Set getBoxes() { // ... } } Company o = new ETCatCompany(); Box boxes = o.getFavBox(); // ok Set boxes = o.getBoxes(); // error As you see, o.getFavBox() will work but o.getBoxes() not, why? Because, you could not throw any concrete Cat into Box, which may possibly corrupt the unknown box. However, you can throw a Box into Set, which may in turns corrupt a Set.

Or, you may think it as: Cat ^ Tiger => Cat Box ^ BigBox => Box Box ^ Box => Box Box ^ BigBox => (Box ^ BigBox) ^ (BigBox ^ BigBox) => Box ^ BigBox => Box ^ BigBox => Box Set ^ Set => Set Set ^ Set => Set Set ^ Set => Set? Extends Cat ^? Extends Tiger =>?

Extends Cat Box ^ BigBox => Box Box ^ Box =>? Extends Box =>? Extends Box Set ^ Set => Set Set ^ Set // You are here.

=> Set Set ^ Set => Set Or more likely in your case, give several operation() calls: Map> x1 = o.operation(); Map> x2 = o.operation(); ... Map> x2 = o.operation(); Then, what's the common type of all? X1 ^ x2 ^ ... ^ xn => Map> ^ Map> ^ ... => Map> ^ ... ... => Map> Well, it's definitely nothing wrong with two wildcards, it's about mathematical induction...

The problem here is that you have two different generic definitions that could be the same: TCV o = new TCVConcrete(); ^^^^^^^^^^^^ Map> map = o.operation(); ^^^^^^^^^^^^^^ While these two could point to the same type, they could also point to two different sub-types of MF. The compiler doesn't like that. You can solve that with a type variable in a dedicated method.

This should work: public void doSomething(){ TCV o = new TCVConcrete(); Map map = o.operation(); }.

There's no way for the generics system to understand that the indicated generic types in the two lines (when applied to a concrete implementation) actually refer to the same concrete type when the compiler does its business. TCV o = new TCVConcrete(); Map> map = o.operation(); In the simple case of Serializable, there are many classes implementing Serializable, so one statement could mean one class, and another statement could mean another, thus this will not compile. You need to create a type scope at the top of the file, and then use the same type for both lines, or implement a subclass of MF and use this for both lines, rather than leaving it as a floating generic type.

Wildcards, which basically say "MF or some unspecified subclass of MF". But that could mean two different subclasses of MF; and that's why the types for the assignment don't match. What you probably want is not to use the wildcards at all.

This should work: TCV o = new TCVConcrete(); Map map = o.operation().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions