Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Passing Enum as Type Parameter to method

Fri Oct 24, 2008 6:15 pm

If I have a class A with a static method f(), and an object x of
type Class<? extends A> which has been assigned B.class, where B
extends A, how can I use x to call B.f()?



Re: Passing Enum as Type Parameter to method

Fri Oct 24, 2008 6:16 pm

to get this approach working you have to use polymorphism in the
declaration of M.g:
Code:

public class M 
{
public static <T extends A> int g (T x) {return T.();} public
static 
<T extends B> int g (T x) {return T.();}
}

class A { public static int f() { return 28; } }
class B extends A { public static int f() { return 42; } }

public class testABC {
public static void main(String[] args) {

A alpha= new A ();
B beta = new B ();
M m = new M ();

int retA = m.(alpha);
int retB = m.(beta);

System.out.print(retA);
System.out.print(retB);
}
}
 


Post a reply
  Related Posts  to : Passing Enum as Type Parameter to method
 Passing arrays as function parameter in java     -  
 argument type of a program's main() method     -  
 Java enum example     -  
 Enum Declaration     -  
 Define enum in java     -  
 how to use enum in jaxb unmarshalling     -  
 extending enum in java     -  
 Array Passing     -  
 Define Enum inside a class     -  
 get url parameter     -  

Topic Tags

Java OOP, Java Basics