0
class a{}
class b extends a{}
class c <? extends b> extends b{}
public class d {
      public static void main(String[] args) {  
        c<c> n = new c<c>(); 
      }
}

I am reading SCJP. I didn't get the concept of generic class with class<?> and class<? extends anything>.....if it means we can pass any class which extends "anything" then why above code is not working?

Please explain this , its very confusing

Regards

Dexture
  • 976
  • 1
  • 11
  • 29
user2985842
  • 437
  • 9
  • 24

2 Answers2

2

It makes no sense to declare a class with a type parameter without ever referring to that parameter. Therefore it is not foreseen to declare such a class. You have to give the type parameter a name.

Change

class c <? extends b> extends b{}

to

class c <T extends b> extends b{}

Within the class c you can now refer to the type parameter T, e.g. to declare methods and variables.

Holger
  • 285,553
  • 42
  • 434
  • 765
2

You didn't give the exact error message, but I guess it's about that you can't use wild cards when you define the type parameters.

Try:

class c <T extends b> extends b{}

Note: It's a convention to start class names with a capital letter, so use:

class C <T extends B> extends B{}
Puce
  • 37,247
  • 13
  • 80
  • 152