• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics and interfaces  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,I am confused why I am getting this error, the error I am getting is type mismatch cannot convert from Animal to Monster<Generic>

now the reason I find this confusing is because in the Monster class I am actually returning a Monster object which is an Animal so how come this assignment one = one.ret(); is not legal as I said I thought it would be legal because I am returning Monster<T>


PS I know the code is a mess to read,but I'm just messing around trying to understand inheritance,interfaces and generics


Animal.interface



Generic.java




Living.interface


Monster.java


 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main.java
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one.ret() returns an Animal, not a Monster. You cannot set a Monster equal to an Animall without casting it, since an Animal might not be a Monster.
You can, however, do it the other way around. You can have
this works because m IS an Animal.

What you were trying is similar to trying this:
The compiler will complain because an Animal is not necessarily a Monster.
Why does your ret() method return an Animal instead of a Monster? There is no good reason for this.
  
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Fred

but I thought since I used this code

return new Monster<T>();

I am returning a monster not an animal,but technically I am returning an Animal because a Monster is an Animal,

so confused as I said I thought I was actually returning a Monster and the reason I could do this was because the return type is Animal and a Monster is an Animal so it's valid but in main I get an error
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also how is this valid?



because Monster returns a Monster with a generic Type but when I created Animal b and assigned it to equal one.ret() I didn't specify the generic type so why didn't an error occur
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is this:
Which explicitly says that ret() returns an Animal. For example, consider what happens if the function was:
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Your code is this:
Which explicitly says that ret() returns an Animal. For example, consider what happens if the function was:


but it returns a Monster which is an animal?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what I don't get is that I'm returning a Monster<T> object which is an animal shouldn't that work?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:but what I don't get is that I'm returning a Monster<T> object which is an animal shouldn't that work?


Correct. Your code in the method is returning a Monster<T> which is an Animal, and since the method is expected to return an Animal, this works.  However, somewhere else in the code, specifically, the calling side, the method is returning an Animal, which may or may not be a Monster<T>, and there is no way for the compiler to confirm it, so, the assignment doesn't compile.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!