Wednesday, 15 February 2012

generics - T and inheritance in Java -



generics - T and inheritance in Java -

i have class static field f:

class { public static string f = null; }

class b:

class b extends { public static string f = "somestring"; }

and typed class method uses field f:

class c<t extends a> { public void somemethod() { string somestring = t.f; // manipulations somestring } }

and code calls it.

c<b> c = new c<b>(); c.somemethod();

and i'm getting null pointer exception when trying manipulate somestring. so, t.f null, t b, should "somestring"! why?

you can't override fields. since extends a, utilize field in a.

add getter in class , b returns f. there, override method in 1 in b.

class { public string getf(){ homecoming null; } } class b { @override public string getf(){ homecoming "somestring"; } }

java generics

No comments:

Post a Comment