Jump to content
Sign in to follow this  
albertlee

one little question on regarding generic method in Java....

Recommended Posts


public <T> void meth1(T extends Something a){..};

public <T extends Something> void meth2(T a){...};

public <T extends Something> void meth3(T extends Something a){...};

 

 

Can any one tell me the difference between these 3???

 

please help

 

thx

Share this post


Link to post
Share on other sites

btw...

 

Can any explain, with examples, the real difference between wilcard type method and generic type method in practice?

 

The only thing it seems to me is that generic type is more concrete, hence we can, for example, add elements to a collection while with wildcard type, we are like assuming to take any thing unknown....

 

please help

Share this post


Link to post
Share on other sites

ok... I understand the 1st question...please ignore...

 

for the second question, an example is:

 

static <T extends Shape> void draw(List<T> l) {drawOnCanvas(l.get(0));}

 

versus

 

static void draw(List<? extends Shape> l) {drawOnCanvas(l.get(0));}

 

 

The above code should elaborate what I mean!

 

please help

 

thx

Share this post


Link to post
Share on other sites

3rd question...

 

Simply put, why cant we declare an array of generic types???

 

please help, along with the 2nd question...

 

thx

Share this post


Link to post
Share on other sites

btw, why I cant compile this code?

public class b{

static class a{}
static class b1 extends a{}
static class c extends b{}


static <A extends b.a> void adds(LinkedList<A> l, A b1){l.[b]add(b1)[/b];}

static public void main(String[] sfg){


LinkedList<c> l = new LinkedList<c>();

adds(l, new c()); }


}

 

and the problem originates from the bold part of the code above....during compile time.

 

Please help

 

thx

Share this post


Link to post
Share on other sites
btw' date=' why I cant compile this code?

public class b{

static class a{}
static class b1 extends a{}
static class c extends b{}


static <A extends b.a> void adds(LinkedList<A> l, A b1){l.[b]add(b1)[/b];}

static public void main(String[] sfg){


LinkedList<c> l = new LinkedList<c>();

adds(l, new c()); }


}

 

and the problem originates from the bold part of the code above....during compile time.

 

Please help

 

thx[/quote']

 

static class c extends b{}

 

to

 

static class c extends b1{}

 

A good example of why you should try naming classes better :P

Share this post


Link to post
Share on other sites

thx Aeternus...but can you help me with other questions on this thread? Which refer to the 2nd, 3rd, and 4th posts here.

 

thx

Share this post


Link to post
Share on other sites

Could you copy-paste the output form the compiler regarding the compile-time errors? Also, I've never seen angle brackets <> in java what’s going on here?

Share this post


Link to post
Share on other sites
Could you copy-paste the output form the compiler regarding the compile-time errors? Also, I've never seen angle brackets <> in java what’s going on here?

 

The angled brackets are for passing type arguments to classes/methods etc when dealing with generics (feature new to java in 1.5).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.