*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Creating Generic classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Creating Generic classes" Watch "Question on Creating Generic classes" New topic
Author

Question on Creating Generic classes

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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 ?


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: May 18, 2010
Posts: 634

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: May 18, 2010
Posts: 634



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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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)...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Creating Generic classes
 
Similar Threads
Polymorphism Problem using Covariant types
Generics question
references and objects
Casting - WHY does Runnable r1 = (Runnable)aa give NO compiler error
Inheritance, Access and Packages