• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Creating Generic classes

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exam LAb




Error:
incompatible types
found : B
required: E
return new B();

1.why am i getting this error even if line 1 is removed ??


------------------------------------------



2.can't get why these two are giving compile time error ?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.why am i getting this error even if line 1 is removed ??

If you remove the return statement completely, you'll get an error as the method should return something. Try return null; and it will work.
2.can't get why these two are giving compile time error ?

You can't use super and wildcards in class declarations...
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
If you remove the return statement completely, you'll get an error as the method should return something. Try return null; and it will work.



My question is why the return new B() not working here ??

Ankit Garg wrote: You can't use super and wildcards in class declarations.



why can't we use super and wildcards ?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return B is not working as its not necessary that E will represent type B. The type of E will depend on what the type is given to an instance of class Monitor.

why can't we use super and wildcards ?

First of all this is a completely wrong syntax <E extends ?>. It cannot compile in any situation. A wildcard can be used as <? super String> but not as you are using it. Wildcard in class level declaration is useless. If you have a class like MyClass<? super String>, its of no use. And you can't use a type name with super, so <E super String> is also an invalid syntax. You can only use wildcard with super in generics...
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit :

return B is not working as its not necessary that E will represent type B. The type of E will depend on what the type is given to an instance of class Monitor.


suppose

Monitor<A> lob=new Monitor<A>();//line 1



why is return new B() not allowed ??

B extends A
so it can be assigned to A i.e.


Here,E can be A or its subtype
i have passed A to E
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you have this class hierarchy

Now if I take your monitor class and use the following code, what will happen
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


still there is Compile time error
donot add more code.
if there is just this code.
Then,why still there is error ?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I gave the answer already. The type of E is not known, so you can't return an instance of B from getSame method. At runtime E may represent any type. The actual type of E may be incompatible with B like in the example that I gave you (where E represents C which is a sibling of B)...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic