java generic method fundamentals -


i running tests better understand java generic methods, , came across following problem. have following simple class:

1    public class someclass<o extends object> { 2     3        o somevar; 4     5        public someclass() { 6           somefunc(new number(1)); 7        } 8     9        public void somefunc(o arg) { 10           // code 11        } 12    } 

as stands, compiler not line 6. eclipse suggests either cast number instance o, or change argument type number on line 9. i avoid both if possible. know modifying class takes care of problem:

1    public class someclass { 2     3        o somevar; 4     5        public someclass() { 6           somefunc(new number(1)); 7        } 8     9        public <o extends object> void somefunc(o arg) { 10           // code 11        } 12    } 

but brings new problem line 3.

so can done original code?

thank time!!!

the problem in first example creating class someclass o can extends object (or object).

for example new someclass<hashmap>();

now compiler have constructor tries pass number, method wants o in case hashmap.

change constructor take argument o something pass somefunc.

public someclass(o something) {   somefunc(something); } 

if know want create , pass around number generic should instead say

class someclass<o extends number> 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -